34 template <
class Key_T,
class Other_T>
39 return high - low + 1;
41 size_t mid{(low + high) / 2};
42 if (x > data[mid].key_)
44 if (x < data[mid].key_)
58 int main(
int argc,
char const *argv[])
66 const std::string full_path_exec{argv[0]};
67 std::string::size_type found = full_path_exec.find_last_of(
"/\\", std::string::npos);
68 const std::string exec_path = full_path_exec.substr(0, found);
69 const std::string exec_filename = full_path_exec.substr(found + 1, std::string::npos);
70 const std::string data_file_name(
"\\ch8_1.result");
73 std::string data_file_full_path{exec_path + data_file_name};
74 std::ofstream fout(data_file_full_path.c_str(), std::ios_base::app);
77 std::cerr <<
"无写权限,测试数据文件生成失败!\n";
84 timeinfo = localtime(&rawtime);
86 std::cout <<
"\nCurrent local time and date: " << asctime(timeinfo) <<
'\n';
87 fout <<
"\nCurrent local time and date: " << asctime(timeinfo) <<
'\n';
92 std::vector<Set::Set<size_t>> vector_size_t;
98 size_t low{10}, high{99}, num_of_points{18};
100 for (
size_t i = 0; i < num_of_points; ++i)
103 iSecret = low + (high - low + 1) * rand() / (RAND_MAX + 1);
108 std::sort(vector_size_t.begin(), vector_size_t.end(), std::less<
Set::Set<size_t>>{});
111 for (
size_t i = 0; i < vector_size_t.size(); ++i)
113 std::cout <<
"binarySearch(" << vector_size_t.at(i).key_ <<
") = " <<
binarySearch(vector_size_t.data(), 0, vector_size_t.size() - 1, vector_size_t.at(i).key_) <<
'\n';
114 fout <<
"binarySearch(" << vector_size_t.at(i).key_ <<
") = " <<
binarySearch(vector_size_t.data(), 0, vector_size_t.size() - 1, vector_size_t.at(i).key_) <<
'\n';
117 catch (
const std::string &e)
119 std::cerr << e <<
'\n';
126 std::cout <<
"\nIt took me " << t <<
" clicks (" << ((float)t) / CLOCKS_PER_SEC <<
" seconds).\n";
127 fout <<
"\nIt took me " << t <<
" clicks (" << ((float)t) / CLOCKS_PER_SEC <<
" seconds).\n";