图  0.1
数据结构_第13章
ch8_2.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 <list>
20 
21 namespace Find
22 {
40  template <class InputIterator, class T>
41  InputIterator find(InputIterator first, InputIterator last, const T &val)
42  {
43  while (first != last)
44  {
45  if (*first == val)
46  return first;
47  ++first;
48  }
49  return last;
50  }
51 } // namespace Find
52 
62 int main(int argc, char const *argv[])
63 {
64  // time
65  clock_t t = clock();
66  time_t rawtime;
67  struct tm *timeinfo;
68 
69  // 获取文件路径,参考:http://www.cplusplus.com/reference/string/string/find_last_of/
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");
75 
76  // 打开测试数据文件
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);
79  if (fout.fail())
80  {
81  std::cerr << "无写权限,测试数据文件生成失败!\n";
82  system("pause");
83  return false;
84  }
85 
86  // output time information
87  time(&rawtime); // Get the current calendar time
88  timeinfo = localtime(&rawtime); // Convert time_t to tm as local time
89  // printf("Current local time and date: %s\n", asctime(timeinfo)); // Convert tm structure to string
90  std::cout << "\nCurrent local time and date: " << asctime(timeinfo) << '\n';
91  fout << "\nCurrent local time and date: " << asctime(timeinfo) << '\n';
92 
93  // 开始测试
94  try
95  {
96  std::list<size_t> ls;
97 
98  /* initialize random seed: */
99  srand(time(NULL));
100 
101  /* generate secret number between low and high: */
102  size_t low{10}, high{99}, num_of_points{18};
103  size_t iSecret;
104  for (size_t i = 0; i < num_of_points; ++i)
105  {
106  iSecret = low + (high - low + 1) * rand() / (RAND_MAX + 1);
107  ls.push_back(iSecret);
108  }
109 
110  // 先排序
111  ls.sort(std::less<size_t>{});
112 
113  // 开始顺序查找
114  for (auto itr = ls.begin(); itr != ls.end(); ++itr)
115  {
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';
118  }
119  }
120  catch (const std::string &e)
121  {
122  std::cerr << e << '\n';
123  fout << e << '\n';
124  }
125 
126  // output time information
127  t = clock() - t;
128  // printf("\nIt took me %ld clicks (%f seconds).\n", t, ((float)t) / CLOCKS_PER_SEC);
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";
131 
132  // 测试结束
133  fout.close();
134  system("pause");
135  return 0;
136 } // main
main
int main(int argc, char const *argv[])
测试程序
Definition: ch8_2.cc:62
Find::find
InputIterator find(InputIterator first, InputIterator last, const T &val)
顺序查找一个顺序单链表
Definition: ch8_2.cc:41
Find
Definition: ch8_2.cc:21