Source of priority_queue02.cpp


  1: //priority_queue02.cpp

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


 11: int main()
 12: {
 13:     cout << "\nThis program illustrates two constructors of the "
 14:         "STL priority-queue class,\nand the assignment of one "
 15:         "priority_queue object to another.";
 16:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 18:     cout << "\nFirst, we create an empty priority_queue object pq1, "
 19:         "then push five values\ninto it, then display those five values in "
 20:         "the default priority order.";
 21:     priority_queue<int> pq1;
 22:     pq1.push(27);
 23:     pq1.push(19);
 24:     pq1.push(35);
 25:     pq1.push(46);
 26:     pq1.push(11);
 27:     while(!pq1.empty())
 28:     {
 29:         cout << "\nPopping ";
 30:         cout << pq1.top();
 31:         pq1.pop();
 32:     }
 33:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 35:     cout << "\nSecond, we create a priority_queue pq2 with four "
 36:         "values from a list,\nthen display those four values, again "
 37:         "in default priority order.";
 38:     int a[] = {11, 44, 33, 22};
 39:     list<int> lst(a, a+4);
 40:     priority_queue<int> pq2(lst.begin(), lst.end());
 41:     //Before displaying pq2, assign it to another priority_queue object.
 42:     priority_queue<int> pq3;
 43:     pq3 = pq2;
 44:     while(!pq2.empty())
 45:     {
 46:         cout << "\nPopping ";
 47:         cout << pq2.top();
 48:         pq2.pop();
 49:     }
 50:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 52:     cout << "\nFinally, we display pq3, which was obtained by "
 53:         "assigning pq2 to pq3,\nimmediately after declaring pq3 as "
 54:         "an empty priority_queue object.";
 55:     while(!pq3.empty())
 56:     {
 57:         cout << "\nPopping ";
 58:         cout << pq3.top();
 59:         pq3.pop();
 60:     }
 61:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 62: }