Advertisement
karbaev

stl-priority_queue.cpp

Mar 4th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | None | 0 0
  1. /* STL queue and priority queue examples */
  2.  
  3. #include <iostream>
  4. #include <queue>
  5.  
  6. using namespace std;
  7.  
  8. /* simple queue example */
  9. void functionA()
  10. {
  11.   queue <int> q;                  //q is a queue of integers
  12.  
  13.   q.push(2);                      //put 2, 5, 3, 2 into the queue
  14.   q.push(5);
  15.   q.push(3);
  16.   q.push(1);
  17.   cout<<"q contains " << q.size() << " elements.\n";
  18.  
  19.   while (!q.empty()) {
  20.     cout << q.front() << endl;    //print out the first element in the queue
  21.     q.pop();                      //remove the first element of the queue
  22.   }
  23. }
  24.  
  25. /* simple priority queue example */
  26. void functionB()
  27. {
  28.   priority_queue <int> pq;      //pq is a priority queue of integers
  29.  
  30.   pq.push(2);                   //put 2, 5, 3, 1 into the priority queue
  31.   pq.push(5);
  32.   pq.push(3);
  33.   pq.push(1);
  34.   cout<<"pq contains " << pq.size() << " elements.\n";
  35.  
  36.   while (!pq.empty()) {
  37.     cout << pq.top() << endl;   //print out the highest priority element
  38.     pq.pop();                   //remove the highest priority element
  39.   }
  40. }
  41.  
  42. int main()
  43. {
  44.   cout << "calling functionA...\n";
  45.   functionA();
  46.   cout << "calling functionB...\n";
  47.   functionB();
  48.  
  49.   return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement