Source of priority_queue04.cpp


  1: //priority_queue04.cpp

  3: #include <iostream>
  4: #include <queue>
  5: using namespace std;

  7: int main()
  8: {
  9:     cout << "\nThis program illustrates how *not* to access "
 10:         "\nthe components of a priority queue.";
 11:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 13:     priority_queue<int> pq;
 14:     pq.push(27);
 15:     pq.push(19);
 16:     pq.push(35);
 17:     pq.push(46);
 18:     pq.push(11);

 20:     cout << "\nThe priority queue pq contains "
 21:         << pq.size() << " elements.";
 22:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 24:     cout << "\nNow we attempt to display the values in the priority "
 25:         "queue using a for-loop\nand checking the size of the priority "
 26:         "queue to determine when to stop.";
 27:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 29:     cout << "\nHere they are, in priority order:\n";
 30:     for (priority_queue<int>::size_type i=1; i<=pq.size(); i++) //Whoops!
 31:     {
 32:         cout << "Popping ";
 33:         cout << pq.top() << "\n";
 34:         pq.pop();
 35:     }
 36:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');
 37: }