集合与静态查找表  0.1
数据结构_第8章
ch8_5.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 
21 namespace Find
22 {
40  template <class InputIterator, class T>
41  InputIterator rfind(InputIterator rbegin, InputIterator rend, const T &val)
42  {
43  const auto rfirst{rbegin};
44  while (rbegin != rend)
45  {
46  if (*rbegin == val)
47  {
48  // 将目标元素向表尾移动一位,使得被查概率高的元素更靠近表尾
49  if (rbegin != rfirst)
50  {
51  std::iter_swap(rbegin, rbegin - 1);
52  --rbegin;
53  }
54  return rbegin;
55  }
56  ++rbegin;
57  }
58  return rend;
59  }
60 } // namespace Find
61 
71 int main(int argc, char const *argv[])
72 {
73  // time
74  clock_t t = clock();
75  time_t rawtime;
76  struct tm *timeinfo;
77 
78  // 获取文件路径,参考:http://www.cplusplus.com/reference/string/string/find_last_of/
79  const std::string full_path_exec{argv[0]};
80  std::string::size_type found = full_path_exec.find_last_of("/\\", std::string::npos);
81  const std::string exec_path = full_path_exec.substr(0, found);
82  const std::string exec_filename = full_path_exec.substr(found + 1, std::string::npos);
83  const std::string data_file_name("\\ch8_5.result");
84 
85  // 打开测试数据文件
86  std::string data_file_full_path{exec_path + data_file_name}; // 数据文件的绝对地址
87  std::ofstream fout(data_file_full_path.c_str(), std::ios_base::app);
88  if (fout.fail())
89  {
90  std::cerr << "无写权限,测试数据文件生成失败!\n";
91  system("pause");
92  return false;
93  }
94 
95  // output time information
96  time(&rawtime); // Get the current calendar time
97  timeinfo = localtime(&rawtime); // Convert time_t to tm as local time
98  // printf("Current local time and date: %s\n", asctime(timeinfo)); // Convert tm structure to string
99  std::cout << "\nCurrent local time and date: " << asctime(timeinfo) << '\n';
100  fout << "\nCurrent local time and date: " << asctime(timeinfo) << '\n';
101 
102  // 开始测试
103  try
104  {
105  std::vector<size_t> vec;
106 
107  /* initialize random seed: */
108  srand(time(NULL));
109 
110  /* generate secret number between low and high: */
111  size_t low{10}, high{25}, num_of_points{16};
112  size_t iSecret;
113  for (size_t i = 0; i < num_of_points; ++i)
114  {
115  iSecret = low + (high - low + 1) * rand() / (RAND_MAX + 1);
116  vec.push_back(iSecret);
117  }
118 
119  // 对vec排序,并去除重复元素
120  std::vector<size_t>::iterator it;
121  std::sort(vec.begin(), vec.end());
122  it = std::unique(vec.begin(), vec.end());
123  vec.resize(std::distance(vec.begin(), it));
124 
125  // 输出vec的元素
126  std::cout << "vec has " << (vec.size()) << " elements:\n";
127  fout << "vec has " << (vec.size()) << " elements:\n";
128  for (auto it = vec.begin(); it != vec.end(); ++it)
129  {
130  std::cout << *it << ", ";
131  fout << *it << ", ";
132  }
133  std::cout << '\n';
134  fout << '\n';
135 
136  // 从表尾反复查找同一元素
137  auto target = vec.at(0);
138  for (size_t i = 0; i < vec.size(); ++i)
139  {
140  auto result = *Find::rfind(vec.rbegin(), vec.rend(), target);
141  std::cout << "*rfind(" << target << ") = " << result << '\n';
142  fout << "*rfind(" << target << ") = " << result << '\n';
143  }
144 
145  // 再次输出vec的元素
146  std::cout << "vec has " << (vec.size()) << " elements:\n";
147  fout << "vec has " << (vec.size()) << " elements:\n";
148  for (auto it = vec.begin(); it != vec.end(); ++it)
149  {
150  std::cout << *it << ", ";
151  fout << *it << ", ";
152  }
153  std::cout << '\n';
154  fout << '\n';
155  }
156  catch (const std::string &e)
157  {
158  std::cerr << e << '\n';
159  fout << e << '\n';
160  }
161  catch (const std::exception &e)
162  {
163  std::cerr << e.what() << '\n';
164  fout << e.what() << '\n';
165  }
166 
167  // output time information
168  t = clock() - t;
169  // printf("\nIt took me %ld clicks (%f seconds).\n", t, ((float)t) / CLOCKS_PER_SEC);
170  std::cout << "\nIt took me " << t << " clicks (" << ((float)t) / CLOCKS_PER_SEC << " seconds).\n";
171  fout << "\nIt took me " << t << " clicks (" << ((float)t) / CLOCKS_PER_SEC << " seconds).\n";
172 
173  // 测试结束
174  fout.close();
175  system("pause");
176  return 0;
177 } // main
Find::rfind
InputIterator rfind(InputIterator rbegin, InputIterator rend, const T &val)
顺序查找一个顺序单链表
Definition: ch8_5.cc:41
main
int main(int argc, char const *argv[])
测试程序
Definition: ch8_5.cc:71
Find
Definition: ch8_2.cc:21