集合与静态查找表  0.1
数据结构_第8章
ch8_3.cc
Go to the documentation of this file.
1 
14 #include <algorithm>
15 #include <cstdlib> /* srand, rand */
16 #include <ctime>
17 #include <fstream>
18 #include <iostream>
19 #include <vector>
20 
29 template <class T>
30 std::vector<T> operator+(const std::vector<T> &vec1,
31  const std::vector<T> &vec2);
32 
41 template <class T>
42 std::vector<T> operator*(const std::vector<T> &vec1,
43  const std::vector<T> &vec2);
44 
53 template <class T>
54 std::vector<T> operator-(const std::vector<T> &vec1,
55  const std::vector<T> &vec2);
56 
66 int main(int argc, char const *argv[]) {
67  // time
68  clock_t t = clock();
69  time_t rawtime;
70  struct tm *timeinfo;
71 
72  // 获取文件路径,参考:http://www.cplusplus.com/reference/string/string/find_last_of/
73  const std::string full_path_exec{argv[0]};
74  std::string::size_type found =
75  full_path_exec.find_last_of("/\\", std::string::npos);
76  const std::string exec_path = full_path_exec.substr(0, found);
77  const std::string exec_filename =
78  full_path_exec.substr(found + 1, std::string::npos);
79  const std::string data_file_name("\\ch8_3.result");
80 
81  // 打开测试数据文件
82  std::string data_file_full_path{exec_path +
83  data_file_name}; // 数据文件的绝对地址
84  std::ofstream fout(data_file_full_path.c_str(), std::ios_base::app);
85  if (fout.fail()) {
86  std::cerr << "无写权限,测试数据文件生成失败!\n";
87  system("pause");
88  return false;
89  }
90 
91  // output time information
92  time(&rawtime); // Get the current calendar time
93  timeinfo = localtime(&rawtime); // Convert time_t to tm as local time
94  // printf("Current local time and date: %s\n", asctime(timeinfo)); // Convert
95  // tm structure to string
96  std::cout << "\nCurrent local time and date: " << asctime(timeinfo) << '\n';
97  fout << "\nCurrent local time and date: " << asctime(timeinfo) << '\n';
98 
99  // 开始测试
100  try {
101  std::vector<size_t> vec1, vec2, vec_union, vec_intersect, vec_diff;
102 
103  /* initialize random seed: */
104  srand(time(NULL));
105 
106  /* generate secret number between low and high: */
107  size_t low{10}, high{25}, num_of_points{16};
108  size_t iSecret;
109  for (size_t i = 0; i < num_of_points; ++i) {
110  iSecret = low + (high - low + 1) * rand() / (RAND_MAX + 1);
111  vec1.push_back(iSecret);
112  iSecret = low + (high - low + 1) * rand() / (RAND_MAX + 1);
113  vec2.push_back(iSecret);
114  }
115 
116  // 对vec1, vec2排序,并去除重复元素
117  std::vector<size_t>::iterator it;
118 
119  std::sort(vec1.begin(), vec1.end());
120  it = std::unique(vec1.begin(), vec1.end());
121  vec1.resize(std::distance(vec1.begin(), it));
122 
123  std::sort(vec2.begin(), vec2.end());
124  it = std::unique(vec2.begin(), vec2.end());
125  vec2.resize(std::distance(vec2.begin(), it));
126 
127  // 输出vec1, vec2的元素
128  std::cout << "set1 has " << (vec1.size()) << " elements:\n";
129  fout << "set1 has " << (vec1.size()) << " elements:\n";
130  for (auto it = vec1.begin(); it != vec1.end(); ++it) {
131  std::cout << *it << ", ";
132  fout << *it << ", ";
133  }
134  std::cout << '\n';
135  fout << '\n';
136 
137  std::cout << "set2 has " << (vec2.size()) << " elements:\n";
138  fout << "set2 has " << (vec2.size()) << " elements:\n";
139  for (auto it = vec2.begin(); it != vec2.end(); ++it) {
140  std::cout << *it << ", ";
141  fout << *it << ", ";
142  }
143  std::cout << '\n';
144  fout << '\n';
145 
146  // 求并,交,差,打印结果
147  vec_union = vec1 + vec2;
148  std::cout << "The union has " << (vec_union.size()) << " elements:\n";
149  fout << "The union has " << (vec_union.size()) << " elements:\n";
150  for (auto it = vec_union.begin(); it != vec_union.end(); ++it) {
151  std::cout << *it << ", ";
152  fout << *it << ", ";
153  }
154  std::cout << '\n';
155  fout << '\n';
156 
157  vec_intersect = vec1 * vec2;
158  std::cout << "The intersection has " << (vec_intersect.size())
159  << " elements:\n";
160  fout << "The intersection has " << (vec_intersect.size()) << " elements:\n";
161  for (auto it = vec_intersect.begin(); it != vec_intersect.end(); ++it) {
162  std::cout << *it << ", ";
163  fout << *it << ", ";
164  }
165  std::cout << '\n';
166  fout << '\n';
167 
168  vec_diff = vec1 - vec2;
169  std::cout << "The difference has " << (vec_diff.size()) << " elements:\n";
170  fout << "The difference has " << (vec_diff.size()) << " elements:\n";
171  for (auto it = vec_diff.begin(); it != vec_diff.end(); ++it) {
172  std::cout << *it << ", ";
173  fout << *it << ", ";
174  }
175  std::cout << '\n';
176  fout << '\n';
177  } catch (const std::string &e) {
178  std::cerr << e << '\n';
179  fout << e << '\n';
180  } catch (const std::exception &e) {
181  std::cerr << e.what() << '\n';
182  fout << e.what() << '\n';
183  }
184 
185  // output time information
186  t = clock() - t;
187  // printf("\nIt took me %ld clicks (%f seconds).\n", t, ((float)t) /
188  // CLOCKS_PER_SEC);
189  std::cout << "\nIt took me " << t << " clicks ("
190  << ((float)t) / CLOCKS_PER_SEC << " seconds).\n";
191  fout << "\nIt took me " << t << " clicks (" << ((float)t) / CLOCKS_PER_SEC
192  << " seconds).\n";
193 
194  // 测试结束
195  fout.close();
196  system("pause");
197  return 0;
198 } // main
199 
200 template <class T>
201 std::vector<T> operator+(const std::vector<T> &vec1,
202  const std::vector<T> &vec2) {
203  std::vector<T> vec_union(vec1.size() + vec2.size());
204 
213  auto first1{vec1.begin()}, last1{vec1.end()};
214 
221  auto first2{vec2.begin()}, last2{vec2.end()};
222 
230  auto result{vec_union.begin()};
231 
232  while (true) {
233  if (first1 == last1) {
234  result = std::copy(first2, last2, result);
235  break;
236  }
237  if (first2 == last2) {
238  result = std::copy(first1, last1, result);
239  break;
240  }
241 
242  if (*first1 < *first2) {
243  *result = *first1;
244  ++first1;
245  } else if (*first2 < *first1) {
246  *result = *first2;
247  ++first2;
248  } else {
249  *result = *first1;
250  ++first1;
251  ++first2;
252  }
253  ++result;
254  }
255  vec_union.resize(result - vec_union.begin());
256  return vec_union;
257 }
258 
259 template <class T>
260 std::vector<T> operator*(const std::vector<T> &vec1,
261  const std::vector<T> &vec2) {
262  std::vector<T> vec_intersect(vec1.size());
263 
272  auto first1{vec1.begin()}, last1{vec1.end()};
273 
280  auto first2{vec2.begin()}, last2{vec2.end()};
281 
289  auto result{vec_intersect.begin()};
290 
291  while (first1 != last1 && first2 != last2) {
292  if (*first1 < *first2)
293  ++first1;
294  else if (*first2 < *first1)
295  ++first2;
296  else {
297  *result = *first1;
298  ++result;
299  ++first1;
300  ++first2;
301  }
302  }
303  vec_intersect.resize(result - vec_intersect.begin());
304  return vec_intersect;
305 }
306 
307 template <class T>
308 std::vector<T> operator-(const std::vector<T> &vec1,
309  const std::vector<T> &vec2) {
310  std::vector<T> vec_diff(vec1.size());
311 
320  auto first1{vec1.begin()}, last1{vec1.end()};
321 
328  auto first2{vec2.begin()}, last2{vec2.end()};
329 
337  auto result{vec_diff.begin()};
338 
339  while (first1 != last1 && first2 != last2) {
340  if (*first1 < *first2) {
341  *result = *first1;
342  ++result;
343  ++first1;
344  } else if (*first2 < *first1)
345  ++first2;
346  else {
347  ++first1;
348  ++first2;
349  }
350  }
351  result = std::copy(first1, last1, result);
352  vec_diff.resize(result - vec_diff.begin());
353  return vec_diff;
354 }
operator-
std::vector< T > operator-(const std::vector< T > &vec1, const std::vector< T > &vec2)
返回两个非降序vector集合的差集vector
Definition: ch8_3.cc:308
operator+
std::vector< T > operator+(const std::vector< T > &vec1, const std::vector< T > &vec2)
返回两个非降序vector集合的并集vector
Definition: ch8_3.cc:201
main
int main(int argc, char const *argv[])
测试程序
Definition: ch8_3.cc:66
operator*
std::vector< T > operator*(const std::vector< T > &vec1, const std::vector< T > &vec2)
返回两个非降序vector集合的交集vector
Definition: ch8_3.cc:260