30 std::vector<T>
operator+(
const std::vector<T> &vec1,
31 const std::vector<T> &vec2);
42 std::vector<T>
operator*(
const std::vector<T> &vec1,
43 const std::vector<T> &vec2);
54 std::vector<T>
operator-(
const std::vector<T> &vec1,
55 const std::vector<T> &vec2);
66 int main(
int argc,
char const *argv[]) {
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");
82 std::string data_file_full_path{exec_path +
84 std::ofstream fout(data_file_full_path.c_str(), std::ios_base::app);
86 std::cerr <<
"无写权限,测试数据文件生成失败!\n";
93 timeinfo = localtime(&rawtime);
96 std::cout <<
"\nCurrent local time and date: " << asctime(timeinfo) <<
'\n';
97 fout <<
"\nCurrent local time and date: " << asctime(timeinfo) <<
'\n';
101 std::vector<size_t> vec1, vec2, vec_union, vec_intersect, vec_diff;
107 size_t low{10}, high{25}, num_of_points{16};
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);
117 std::vector<size_t>::iterator it;
119 std::sort(vec1.begin(), vec1.end());
120 it = std::unique(vec1.begin(), vec1.end());
121 vec1.resize(std::distance(vec1.begin(), it));
123 std::sort(vec2.begin(), vec2.end());
124 it = std::unique(vec2.begin(), vec2.end());
125 vec2.resize(std::distance(vec2.begin(), it));
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 <<
", ";
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 <<
", ";
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 <<
", ";
157 vec_intersect = vec1 * vec2;
158 std::cout <<
"The intersection has " << (vec_intersect.size())
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 <<
", ";
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 <<
", ";
177 }
catch (
const std::string &e) {
178 std::cerr << e <<
'\n';
180 }
catch (
const std::exception &e) {
181 std::cerr << e.what() <<
'\n';
182 fout << e.what() <<
'\n';
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
202 const std::vector<T> &vec2) {
203 std::vector<T> vec_union(vec1.size() + vec2.size());
213 auto first1{vec1.begin()}, last1{vec1.end()};
221 auto first2{vec2.begin()}, last2{vec2.end()};
230 auto result{vec_union.begin()};
233 if (first1 == last1) {
234 result = std::copy(first2, last2, result);
237 if (first2 == last2) {
238 result = std::copy(first1, last1, result);
242 if (*first1 < *first2) {
245 }
else if (*first2 < *first1) {
255 vec_union.resize(result - vec_union.begin());
261 const std::vector<T> &vec2) {
262 std::vector<T> vec_intersect(vec1.size());
272 auto first1{vec1.begin()}, last1{vec1.end()};
280 auto first2{vec2.begin()}, last2{vec2.end()};
289 auto result{vec_intersect.begin()};
291 while (first1 != last1 && first2 != last2) {
292 if (*first1 < *first2)
294 else if (*first2 < *first1)
303 vec_intersect.resize(result - vec_intersect.begin());
304 return vec_intersect;
309 const std::vector<T> &vec2) {
310 std::vector<T> vec_diff(vec1.size());
320 auto first1{vec1.begin()}, last1{vec1.end()};
328 auto first2{vec2.begin()}, last2{vec2.end()};
337 auto result{vec_diff.begin()};
339 while (first1 != last1 && first2 != last2) {
340 if (*first1 < *first2) {
344 }
else if (*first2 < *first1)
351 result = std::copy(first1, last1, result);
352 vec_diff.resize(result - vec_diff.begin());