C++/C++11中std::priority_queue的使用

std::priority_queue:在优先队列中,优先级高的元素先出队列,并非按照先进先出的要求,类似一个堆(heap)。其模板声明带有三个参数,priority_queue, 其中Type为数据类型,Container为保存数据的容器,Functional为元素比较方式。Container必须是用数组实现的容器,比如 vector, deque. STL里面默认用的是vector. 比较方式默认用operator< , 所以如果把后面两个参数缺省的话,优先队列就是大顶堆,队头元素最大。

std::priority_queue: priority queues are a type of container adaptors, specifically designed such that its first element is always the greatest of the elements it contains, according to some strict weak ordering criterion. This context is similar to a heap, where elements can be inserted at any moment, and only the max heap element can be retrieved (the one at the top in the priority queue). Priority queues are implemented as container adaptors, which are classes that use an encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access its elements. Elements are popped from the "back" of the specific container, which is known as the top of the priority queue.

A priority queue is a queue data structure that has the particular property of being sorted by priority. You can decide what priority to give items depending on the data type.

下面是从其他文章中copy的测试代码,详细内容介绍可以参考对应的reference:

 

 
  1. #include "priority_queue.hpp"

  2. #include

  3. #include // std::priority_queue

  4. #include

  5. #include

  6. #include // std::greater

  7. #include

  8. #include

  9.  
  10. //template < class T,

  11. // class Container = vector,

  12. // class Compare = less >

  13. //class priority_queue;

  14.  
  15. ///

  16. // reference: http://www.cplusplus.com/reference/queue/priority_queue/

  17. int test_priority_queue_1()

  18. {

  19. { // std::priority_queue::push: Inserts a new element in the priority_queue.

  20. // The content of this new element is initialized to val. inserts element and sorts the underlying container

  21. // std::priority_queue::pop: Removes the element on top of the priority_queue, removes the top element,

  22. // effectively reducing its size by one. The element removed is the one with the highest value.

  23. // std::priority_queue::empty: Test whether container is empty

  24. std::priority_queue mypq;

  25.  
  26. mypq.push(30);

  27. mypq.push(100);

  28. mypq.push(25);

  29. mypq.push(40);

  30.  
  31. std::cout << "Popping out elements...";

  32. while (!mypq.empty()) {

  33. std::cout << ' ' << mypq.top();

  34. mypq.pop();

  35. }

  36. std::cout << '\n';

  37. }

  38.  
  39. { // std::priority_queue::emplace: C++11, Adds a new element to the priority_queue.

  40. // This new element is constructed in place passing args as the arguments for its constructor.

  41. std::priority_queue mypq;

  42.  
  43. mypq.emplace("orange");

  44. mypq.emplace("strawberry");

  45. mypq.emplace("apple");

  46. mypq.emplace("pear");

  47.  
  48. std::cout << "mypq contains:";

  49. while (!mypq.empty()) {

  50. std::cout << ' ' << mypq.top();

  51. mypq.pop();

  52. }

  53. std::cout << '\n';

  54. }

  55.  
  56. {// std::priority_queue::size: Returns the number of elements in the priority_queue

  57. std::priority_queue myints;

  58. std::cout << "0. size: " << myints.size() << '\n';

  59.  
  60. for (int i = 0; i < 5; i++) myints.push(i);

  61. std::cout << "1. size: " << myints.size() << '\n';

  62.  
  63. myints.pop();

  64. std::cout << "2. size: " << myints.size() << '\n';

  65. }

  66.  
  67. { // std::priority_queue::top: Returns a constant reference to the top element in the priority_queue.

  68. // The top element is the element that compares higher in the priority_queue,

  69. // and the next that is removed from the container when priority_queue::pop is called.

  70. std::priority_queue mypq;

  71.  
  72. mypq.push(10);

  73. mypq.push(20);

  74. mypq.push(15);

  75.  
  76. std::cout << "mypq.top() is now " << mypq.top() << '\n';

  77. }

  78.  
  79. { // std::priority_queue::swap: C++11, Exchanges the contents of the container adaptor by those of x,

  80. // swapping both the underlying container value and their comparison function using

  81. // the corresponding swap non-member functions (unqualified)

  82. // std::swap (priority_queue): Exchange contents of priority queues

  83. std::priority_queue foo, bar;

  84. foo.push(15); foo.push(30); foo.push(10);

  85. bar.push(101); bar.push(202);

  86.  
  87. foo.swap(bar);

  88. //std::swap(foo, bar);

  89.  
  90. std::cout << "size of foo: " << foo.size() << '\n';

  91. std::cout << "size of bar: " << bar.size() << '\n';

  92. }

  93.  
  94. return 0;

  95. }

  96.  
  97. //

  98. // reference: http://en.cppreference.com/w/cpp/container/priority_queue

  99. template

  100. static void print_queue(T& q) {

  101. while (!q.empty()) {

  102. std::cout << q.top() << " ";

  103. q.pop();

  104. }

  105. std::cout << '\n';

  106. }

  107.  
  108. int test_priority_queue_2()

  109. {

  110. std::priority_queue q;

  111. for (int n : {1, 8, 5, 6, 3, 4, 0, 9, 7, 2})

  112. q.push(n);

  113. print_queue(q);

  114.  
  115. std::priority_queue, std::greater > q2;

  116. for (int n : {1, 8, 5, 6, 3, 4, 0, 9, 7, 2})

  117. q2.push(n);

  118. print_queue(q2);

  119.  
  120. // Using lambda to compare elements.

  121. auto cmp = [](int left, int right) { return (left ^ 1) < (right ^ 1); };

  122. std::priority_queue, decltype(cmp)> q3(cmp);

  123. for (int n : {1, 8, 5, 6, 3, 4, 0, 9, 7, 2})

  124. q3.push(n);

  125. print_queue(q3);

  126.  
  127. return 0;

  128. }

  129.  
  130. /

  131. // reference: http://www.technical-recipes.com/2011/priority-queues-and-min-priority-queues-in-c/

  132. struct compare {

  133. bool operator()(const int& l, const int& r) {

  134. return l > r;

  135. }

  136. };

  137.  
  138. int test_priority_queue_3()

  139. {

  140. using namespace std;

  141. priority_queue, compare > pq;

  142.  
  143. pq.push(3);

  144. pq.push(5);

  145. pq.push(1);

  146. pq.push(8);

  147.  
  148.  
  149. std::cout << "pq contains:";

  150. while (!pq.empty()) {

  151. std::cout << ' ' << pq.top();

  152. pq.pop();

  153. }

  154. std::cout << '\n';

  155.  
  156. return 0;

  157. }

  158.  
  159. /

  160. // reference: https://msdn.microsoft.com/en-us/library/4ef4dae9.aspx

  161. int test_priority_queue_4()

  162. {

  163. { // priority_queue::container_type: A type that provides the base container to be adapted.

  164. // priority_queue::empty: Tests if a priority_queue is empty.

  165. // true if the priority_queue is empty; false if the priority_queue is nonempty.

  166. using namespace std;

  167.  
  168. // Declares priority_queues with default deque base container

  169. priority_queue q1, s2;

  170. q1.push(1);

  171.  
  172. if (q1.empty()) cout << "The priority_queue q1 is empty." << endl;

  173. else cout << "The priority_queue q1 is not empty." << endl;

  174.  
  175. if (s2.empty()) cout << "The priority_queue s2 is empty." << endl;

  176. else cout << "The priority_queue s2 is not empty." << endl;

  177. }

  178.  
  179. { // priority_queue::pop: Removes the largest element of the priority_queue from the top position.

  180. using namespace std;

  181. priority_queue q1, s2;

  182.  
  183. q1.push(10);

  184. q1.push(5);

  185. q1.push(30);

  186.  
  187. priority_queue ::size_type i, iii;

  188. i = q1.size();

  189. cout << "The priority_queue length is " << i << "." << endl;

  190.  
  191. const int& ii = q1.top();

  192. cout << "The element at the top of the priority_queue is " << ii << "." << endl;

  193. q1.pop();

  194.  
  195. iii = q1.size();

  196. cout << "After a pop, the priority_queue length is " << iii << "." << endl;

  197. const int& iv = q1.top();

  198. cout << "After a pop, the element at the top of the " << "priority_queue is " << iv << "." << endl;

  199. }

  200.  
  201. { // priority_queue::priority_queue: Constructs a priority_queue that is empty or

  202. // that is a copy of a range of a base container object or of another priority_queue

  203. using namespace std;

  204.  
  205. // The first member function declares priority_queue

  206. // with a default vector base container

  207. priority_queue q1;

  208. cout << "q1 = ( ";

  209. while (!q1.empty()) {

  210. cout << q1.top() << " ";

  211. q1.pop();

  212. }

  213. cout << ")" << endl;

  214.  
  215. // Explicitly declares a priority_queue with nondefault

  216. // deque base container

  217. priority_queue > q2;

  218. q2.push(5);

  219. q2.push(15);

  220. q2.push(10);

  221. cout << "q2 = ( ";

  222. while (!q2.empty()) {

  223. cout << q2.top() << " ";

  224. q2.pop();

  225. }

  226. cout << ")" << endl;

  227.  
  228. // This method of printing out the elements of a priority_queue

  229. // removes the elements from the priority queue, leaving it empty

  230. cout << "After printing, q2 has " << q2.size() << " elements." << endl;

  231.  
  232. // The third member function declares a priority_queue

  233. // with a vector base container and specifies that the comparison

  234. // function greater is to be used for ordering elements

  235. priority_queue , greater > q3;

  236. q3.push(2);

  237. q3.push(1);

  238. q3.push(3);

  239. cout << "q3 = ( ";

  240. while (!q3.empty()) {

  241. cout << q3.top() << " ";

  242. q3.pop();

  243. }

  244. cout << ")" << endl;

  245.  
  246. // The fourth member function declares a priority_queue and

  247. // initializes it with elements copied from another container:

  248. // first, inserting elements into q1, then copying q1 elements into q4

  249. q1.push(100);

  250. q1.push(200);

  251. q1.push(5);

  252. priority_queue q4(q1);

  253. cout << "q4 = ( ";

  254. while (!q4.empty()) {

  255. cout << q4.top() << " ";

  256. q4.pop();

  257. }

  258. cout << ")" << endl;

  259.  
  260. // Creates an auxiliary vector object v5 to be used to initialize q5

  261. vector v5;

  262. vector ::iterator v5_Iter;

  263. v5.push_back(10);

  264. v5.push_back(30);

  265. v5.push_back(20);

  266. cout << "v5 = ( ";

  267. for (v5_Iter = v5.begin(); v5_Iter != v5.end(); v5_Iter++)

  268. cout << *v5_Iter << " ";

  269. cout << ")" << endl;

  270.  
  271. // The fifth member function declares and

  272. // initializes a priority_queue q5 by copying the

  273. // range v5[ first, last) from vector v5

  274. priority_queue q5(v5.begin(), v5.begin() + 2);

  275. cout << "q5 = ( ";

  276. while (!q5.empty()) {

  277. cout << q5.top() << " ";

  278. q5.pop();

  279. }

  280. cout << ")" << endl;

  281.  
  282. // The sixth member function declares a priority_queue q6

  283. // with a comparison function greater and initializes q6

  284. // by copying the range v5[ first, last) from vector v5

  285. priority_queue , greater >

  286. q6(v5.begin(), v5.begin() + 2);

  287. cout << "q6 = ( ";

  288. while (!q6.empty()) {

  289. cout << q6.top() << " ";

  290. q6.pop();

  291. }

  292. cout << ")" << endl;

  293. }

  294.  
  295. { // priority_queue::push: Adds an element to the priority queue based on the priority of the element from operator<.

  296. using namespace std;

  297. priority_queue q1;

  298.  
  299. q1.push(10);

  300. q1.push(30);

  301. q1.push(20);

  302.  
  303. priority_queue::size_type i;

  304. i = q1.size();

  305. cout << "The priority_queue length is " << i << "." << endl;

  306.  
  307. const int& ii = q1.top();

  308. cout << "The element at the top of the priority_queue is " << ii << "." << endl;

  309. }

  310.  
  311. { // priority_queue::size: Returns the number of elements in the priority_queue.

  312. // priority_queue::size_type: An unsigned integer type that can represent the number of elements in a priority_queue.

  313. using namespace std;

  314. priority_queue q1, q2;

  315. priority_queue ::size_type i;

  316.  
  317. q1.push(1);

  318. i = q1.size();

  319. cout << "The priority_queue length is " << i << "." << endl;

  320.  
  321. q1.push(2);

  322. i = q1.size();

  323. cout << "The priority_queue length is now " << i << "." << endl;

  324. }

  325.  
  326. { // priority_queue::top: Returns a const reference to the largest element at the top of the priority_queue.

  327. using namespace std;

  328. priority_queue q1;

  329.  
  330. q1.push(10);

  331. q1.push(30);

  332. q1.push(20);

  333.  
  334. priority_queue::size_type i;

  335. i = q1.size();

  336. cout << "The priority_queue length is " << i << "." << endl;

  337.  
  338. const int& ii = q1.top();

  339. cout << "The element at the top of the priority_queue is " << ii << "." << endl;

  340. }

  341.  
  342. { // priority_queue::value_type: A type that represents the type of object stored as an element in a priority_queue.

  343. using namespace std;

  344.  
  345. // Declares priority_queues with default deque base container

  346. priority_queue::value_type AnInt;

  347.  
  348. AnInt = 69;

  349. cout << "The value_type is AnInt = " << AnInt << endl;

  350.  
  351. priority_queue q1;

  352. q1.push(AnInt);

  353. cout << "The element at the top of the priority_queue is " << q1.top() << "." << endl;

  354. }

  355.  
  356. return 0;

  357. }


GitHub:https://github.com/fengbingchun/Messy_Test

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/fengbingchun/article/details/70505628

你可能感兴趣的:(C++,C)