优先级队列  0.1
数据结构_第7章
test0.cc
Go to the documentation of this file.
1 
14 #include "BinaryHeap.h"
15 #include <cstdlib> /* srand, rand */
16 #include <ctime>
17 #include <fstream>
18 
28 int main(int argc, char const *argv[])
29 {
30  // time
31  clock_t t = clock();
32  time_t rawtime;
33  struct tm *timeinfo;
34 
35  // 获取文件路径,参考:http://www.cplusplus.com/reference/string/string/find_last_of/
36  const std::string full_path_exec{argv[0]};
37  std::string::size_type found = full_path_exec.find_last_of("/\\", std::string::npos);
38  const std::string exec_path = full_path_exec.substr(0, found);
39  const std::string exec_filename = full_path_exec.substr(found + 1, std::string::npos);
40  const std::string data_file_name("\\test0.result");
41 
42  // 打开测试数据文件
43  std::string data_file_full_path{exec_path + data_file_name}; // 数据文件的绝对地址
44  std::ofstream fout(data_file_full_path.c_str(), std::ios_base::app);
45  if (fout.fail())
46  {
47  std::cerr << "无写权限,测试数据文件生成失败!\n";
48 
49  system("pause");
50  return false;
51  }
52 
53  // output time information
54  time(&rawtime); // Get the current calendar time
55  timeinfo = localtime(&rawtime); // Convert time_t to tm as local time
56  // printf("Current local time and date: %s\n", asctime(timeinfo)); // Convert tm structure to string
57  std::cout << "\nCurrent local time and date: " << asctime(timeinfo) << '\n';
58  fout << "\nCurrent local time and date: " << asctime(timeinfo) << '\n';
59 
60  // 开始测试
61  try
62  {
63  int low{10}, high{99}, num_of_points{18};
64  List::seqList<std::string> string_array(num_of_points);
65  /* initialize random seed: */
66  srand(time(NULL));
67 
68  /* generate secret number between 1 and 10: */
69  int iSecret;
70  for (int i = 0; i < num_of_points; ++i)
71  {
72  iSecret = low + (high - low + 1) * rand() / (RAND_MAX + 1);
73  string_array.push_back(std::to_string(iSecret));
74  }
75 
76  string_array.traverse(std::cout);
77  string_array.traverse(fout);
78 
79  // 从源数组建立一个最小堆
80  Queue::BinaryHeap priority_queue(string_array, std::greater<std::string>{});
81 
82  // 依次出队
83  std::string str;
84  while (!priority_queue.empty())
85  {
86  str = priority_queue.top();
87  priority_queue.pop(&str);
88  std::cout << str << ' ';
89  fout << str << ' ';
90  }
91 
92  std::cout << "\n队空? "
93  << std::boolalpha << priority_queue.empty() << '\n';
94  fout << "\n队空? "
95  << std::boolalpha << priority_queue.empty() << '\n';
96 
97  // 逐元素入队建立最小堆
98  for (int i = 0; i < num_of_points; ++i)
99  {
100  priority_queue.push(string_array[i]);
101  }
102 
103  // 依次出队
104  while (!priority_queue.empty())
105  {
106  str = priority_queue.top();
107  priority_queue.pop(&str);
108  std::cout << str << ' ';
109  fout << str << ' ';
110  }
111  }
112  catch (const std::string &e)
113  {
114  std::cerr << e << '\n';
115  fout << e << '\n';
116  }
117 
118  // output time information
119  t = clock() - t;
120  // printf("\nIt took me %ld clicks (%f seconds).\n", t, ((float)t) / CLOCKS_PER_SEC);
121  std::cout << "\nIt took me " << t << " clicks (" << ((float)t) / CLOCKS_PER_SEC << " seconds).\n";
122  fout << "\nIt took me " << t << " clicks (" << ((float)t) / CLOCKS_PER_SEC << " seconds).\n";
123 
124  // 测试结束
125  fout.close();
126  system("pause");
127  return 0;
128 }
main
int main(int argc, char const *argv[])
测试程序
Definition: test0.cc:28
List::seqList
Definition: seqList.h:25
List::seqList::push_back
void push_back(const T &x)
在表尾插入新元素
Definition: seqList.h:439
Queue::BinaryHeap
Priority queue.
Definition: BinaryHeap.h:40
BinaryHeap.h
优先级队列的接口和实现
List::seqList::traverse
virtual void traverse(std::ostream &out=std::cout) const
Definition: seqList.h:322
Queue::BinaryHeap::top
const_reference top() const
返回队头元素值
Definition: BinaryHeap.h:215