40 template <
class InputIterator,
class T>
41 InputIterator
find(InputIterator first, InputIterator last,
const T &val)
62 int main(
int argc,
char const *argv[])
70 const std::string full_path_exec{argv[0]};
71 std::string::size_type found = full_path_exec.find_last_of(
"/\\", std::string::npos);
72 const std::string exec_path = full_path_exec.substr(0, found);
73 const std::string exec_filename = full_path_exec.substr(found + 1, std::string::npos);
74 const std::string data_file_name(
"\\ch8_2.result");
77 std::string data_file_full_path{exec_path + data_file_name};
78 std::ofstream fout(data_file_full_path.c_str(), std::ios_base::app);
81 std::cerr <<
"无写权限,测试数据文件生成失败!\n";
88 timeinfo = localtime(&rawtime);
90 std::cout <<
"\nCurrent local time and date: " << asctime(timeinfo) <<
'\n';
91 fout <<
"\nCurrent local time and date: " << asctime(timeinfo) <<
'\n';
102 size_t low{10}, high{99}, num_of_points{18};
104 for (
size_t i = 0; i < num_of_points; ++i)
106 iSecret = low + (high - low + 1) * rand() / (RAND_MAX + 1);
107 ls.push_back(iSecret);
111 ls.sort(std::less<size_t>{});
114 for (
auto itr = ls.begin(); itr != ls.end(); ++itr)
116 std::cout <<
"*find(" << *itr <<
") = " << *
Find::find(ls.begin(), ls.end(), *itr) <<
'\n';
117 fout <<
"*find(" << *itr <<
") = " << *
Find::find(ls.begin(), ls.end(), *itr) <<
'\n';
120 catch (
const std::string &e)
122 std::cerr << e <<
'\n';
129 std::cout <<
"\nIt took me " << t <<
" clicks (" << ((float)t) / CLOCKS_PER_SEC <<
" seconds).\n";
130 fout <<
"\nIt took me " << t <<
" clicks (" << ((float)t) / CLOCKS_PER_SEC <<
" seconds).\n";