图  0.1
数据结构_第13章
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, const std::vector<T> &vec2);
31 
40 template <class T>
41 std::vector<T> operator*(const std::vector<T> &vec1, const std::vector<T> &vec2);
42 
51 template <class T>
52 std::vector<T> operator-(const std::vector<T> &vec1, const std::vector<T> &vec2);
53 
63 int main(int argc, char const *argv[])
64 {
65  // time
66  clock_t t = clock();
67  time_t rawtime;
68  struct tm *timeinfo;
69 
70  // 获取文件路径,参考:http://www.cplusplus.com/reference/string/string/find_last_of/
71  const std::string full_path_exec{argv[0]};
72  std::string::size_type found = full_path_exec.find_last_of("/\\", std::string::npos);
73  const std::string exec_path = full_path_exec.substr(0, found);
74  const std::string exec_filename = full_path_exec.substr(found + 1, std::string::npos);
75  const std::string data_file_name("\\ch8_3.result");
76 
77  // 打开测试数据文件
78  std::string data_file_full_path{exec_path + data_file_name}; // 数据文件的绝对地址
79  std::ofstream fout(data_file_full_path.c_str(), std::ios_base::app);
80  if (fout.fail())
81  {
82  std::cerr << "无写权限,测试数据文件生成失败!\n";
83  system("pause");
84  return false;
85  }
86 
87  // output time information
88  time(&rawtime); // Get the current calendar time
89  timeinfo = localtime(&rawtime); // Convert time_t to tm as local time
90  // printf("Current local time and date: %s\n", asctime(timeinfo)); // Convert tm structure to string
91  std::cout << "\nCurrent local time and date: " << asctime(timeinfo) << '\n';
92  fout << "\nCurrent local time and date: " << asctime(timeinfo) << '\n';
93 
94  // 开始测试
95  try
96  {
97  std::vector<size_t> vec1, vec2, vec_union, vec_intersect, vec_diff;
98 
99  /* initialize random seed: */
100  srand(time(NULL));
101 
102  /* generate secret number between low and high: */
103  size_t low{10}, high{25}, num_of_points{16};
104  size_t iSecret;
105  for (size_t i = 0; i < num_of_points; ++i)
106  {
107  iSecret = low + (high - low + 1) * rand() / (RAND_MAX + 1);
108  vec1.push_back(iSecret);
109  iSecret = low + (high - low + 1) * rand() / (RAND_MAX + 1);
110  vec2.push_back(iSecret);
111  }
112 
113  // 对vec1, vec2排序,并去除重复元素
114  std::vector<size_t>::iterator it;
115 
116  std::sort(vec1.begin(), vec1.end());
117  it = std::unique(vec1.begin(), vec1.end());
118  vec1.resize(std::distance(vec1.begin(), it));
119 
120  std::sort(vec2.begin(), vec2.end());
121  it = std::unique(vec2.begin(), vec2.end());
122  vec2.resize(std::distance(vec2.begin(), it));
123 
124  // 输出vec1, vec2的元素
125  std::cout << "set1 has " << (vec1.size()) << " elements:\n";
126  fout << "set1 has " << (vec1.size()) << " elements:\n";
127  for (auto it = vec1.begin(); it != vec1.end(); ++it)
128  {
129  std::cout << *it << ", ";
130  fout << *it << ", ";
131  }
132  std::cout << '\n';
133  fout << '\n';
134 
135  std::cout << "set2 has " << (vec2.size()) << " elements:\n";
136  fout << "set2 has " << (vec2.size()) << " elements:\n";
137  for (auto it = vec2.begin(); it != vec2.end(); ++it)
138  {
139  std::cout << *it << ", ";
140  fout << *it << ", ";
141  }
142  std::cout << '\n';
143  fout << '\n';
144 
145  // 求并,交,差,打印结果
146  vec_union = vec1 + vec2;
147  std::cout << "The union has " << (vec_union.size()) << " elements:\n";
148  fout << "The union has " << (vec_union.size()) << " elements:\n";
149  for (auto it = vec_union.begin(); it != vec_union.end(); ++it)
150  {
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()) << " elements:\n";
159  fout << "The intersection has " << (vec_intersect.size()) << " elements:\n";
160  for (auto it = vec_intersect.begin(); it != vec_intersect.end(); ++it)
161  {
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  {
173  std::cout << *it << ", ";
174  fout << *it << ", ";
175  }
176  std::cout << '\n';
177  fout << '\n';
178  }
179  catch (const std::string &e)
180  {
181  std::cerr << e << '\n';
182  fout << e << '\n';
183  }
184  catch (const std::exception &e)
185  {
186  std::cerr << e.what() << '\n';
187  fout << e.what() << '\n';
188  }
189 
190  // output time information
191  t = clock() - t;
192  // printf("\nIt took me %ld clicks (%f seconds).\n", t, ((float)t) / CLOCKS_PER_SEC);
193  std::cout << "\nIt took me " << t << " clicks (" << ((float)t) / CLOCKS_PER_SEC << " seconds).\n";
194  fout << "\nIt took me " << t << " clicks (" << ((float)t) / CLOCKS_PER_SEC << " seconds).\n";
195 
196  // 测试结束
197  fout.close();
198  system("pause");
199  return 0;
200 } // main
201 
202 template <class T>
203 std::vector<T> operator+(const std::vector<T> &vec1, const std::vector<T> &vec2)
204 {
205  std::vector<T> vec_union(vec1.size() + vec2.size());
206 
212  auto first1{vec1.begin()}, last1{vec1.end()};
213 
219  auto first2{vec2.begin()}, last2{vec2.end()};
220 
226  auto result{vec_union.begin()};
227 
228  while (true)
229  {
230  if (first1 == last1)
231  {
232  result = std::copy(first2, last2, result);
233  break;
234  }
235  if (first2 == last2)
236  {
237  result = std::copy(first1, last1, result);
238  break;
239  }
240 
241  if (*first1 < *first2)
242  {
243  *result = *first1;
244  ++first1;
245  }
246  else if (*first2 < *first1)
247  {
248  *result = *first2;
249  ++first2;
250  }
251  else
252  {
253  *result = *first1;
254  ++first1;
255  ++first2;
256  }
257  ++result;
258  }
259  vec_union.resize(result - vec_union.begin());
260  return vec_union;
261 }
262 
263 template <class T>
264 std::vector<T> operator*(const std::vector<T> &vec1, const std::vector<T> &vec2)
265 {
266  std::vector<T> vec_intersect(vec1.size());
267 
273  auto first1{vec1.begin()}, last1{vec1.end()};
274 
280  auto first2{vec2.begin()}, last2{vec2.end()};
281 
287  auto result{vec_intersect.begin()};
288 
289  while (first1 != last1 && first2 != last2)
290  {
291  if (*first1 < *first2)
292  ++first1;
293  else if (*first2 < *first1)
294  ++first2;
295  else
296  {
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, const std::vector<T> &vec2)
309 {
310  std::vector<T> vec_diff(vec1.size());
311 
317  auto first1{vec1.begin()}, last1{vec1.end()};
318 
324  auto first2{vec2.begin()}, last2{vec2.end()};
325 
331  auto result{vec_diff.begin()};
332 
333  while (first1 != last1 && first2 != last2)
334  {
335  if (*first1 < *first2)
336  {
337  *result = *first1;
338  ++result;
339  ++first1;
340  }
341  else if (*first2 < *first1)
342  ++first2;
343  else
344  {
345  ++first1;
346  ++first2;
347  }
348  }
349  result = std::copy(first1, last1, result);
350  vec_diff.resize(result - vec_diff.begin());
351  return vec_diff;
352 }
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:203
main
int main(int argc, char const *argv[])
测试程序
Definition: ch8_3.cc:63
operator*
std::vector< T > operator*(const std::vector< T > &vec1, const std::vector< T > &vec2)
返回两个非降序vector集合的交集vector
Definition: ch8_3.cc:264