Source of priority_queue03.cpp


  1: //priority_queue03.cpp

  3: #include <iostream>
  4: #include <queue>
  5: #include <vector>
  6: #include <functional>
  7: using namespace std;

  9: int main()
 10: {
 11:     cout << "\nThis program illustrates how to define a priority_queue "
 12:         "object with the\npriority determined by a built-in \"function "
 13:         "object\", and also how to alter\nthe underlying container used "
 14:         "by the priority_queue object.";
 15:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 17:     priority_queue<int, vector<int>, greater<int>> pq1;
 18:     pq1.push(27);
 19:     pq1.push(19);
 20:     pq1.push(35);
 21:     pq1.push(46);
 22:     pq1.push(11);

 24:     cout << "\nThe priority queue pq1 contains "
 25:         << pq1.size() << " values.";
 26:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 28:     cout << "\nHere they are, in priority order:\n";
 29:     while(!pq1.empty())
 30:     {
 31:         cout << "Popping ";
 32:         cout << pq1.top() << "\n";
 33:         pq1.pop();
 34:     }
 35:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');

 37:     priority_queue<double, deque<double>, greater<double>> pq2;
 38:     pq2.push(2.7);
 39:     pq2.push(1.9);
 40:     pq2.push(3.5);
 41:     pq2.push(4.6);
 42:     pq2.push(1.1);

 44:     cout << "\nThe priority queue pq2 contains "
 45:         << pq2.size() << " values.";
 46:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 48:     cout << "\nHere they are, in priority order:\n";
 49:     while(!pq2.empty())
 50:     {
 51:         cout << "Popping ";
 52:         cout << pq2.top() << "\n";
 53:         pq2.pop();
 54:     }
 55:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');
 56: }