Source of queue03.cpp


  1: //queue03.cpp

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

  8: int main()
  9: {
 10:     cout << "\nThis program illustrates the assignment of one queue "
 11:         "to another,\nand the comparison of queues.";
 12:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 14:     queue<int> q1;
 15:     q1.push(1);
 16:     q1.push(2);
 17:     q1.push(3);
 18:     q1.push(4);
 19:     cout << "\nThe queue q1 contains " << q1.size() << " values.";
 20:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 22:     cout << "\nNow we create three new empty queues-q2, q3, q4-"
 23:         "and assign q1 to all three.\nThen we display the contents of "
 24:         "q1 to show what q2, q3 and q4 all contain.\nNote that the "
 25:         "process of displaying the values in q1 empties q1.";
 26:     queue<int> q2, q3, q4;
 27:     q4 = q3 = q2 = q1;
 28:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 30:     cout << "\nHere are the values of q1, in \"FIFO\" order:\n";
 31:     //This is the proper way to access the elements of a queue:
 32:     while(!q1.empty())
 33:     {
 34:         cout << "Popping: ";
 35:         cout << q1.front() << "\n";
 36:         q1.pop();
 37:     }
 38:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');

 40:     cout << "\nNext we display the contents of q2 to confirm that "
 41:         "q1 did get assigned to q2.";
 42:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 44:     cout << "\nHere are the values of q2, in \"FIFO\" order:\n";
 45:     while(!q2.empty())
 46:     {
 47:         cout << "Popping: ";
 48:         cout << q2.front() << "\n";
 49:         q2.pop();
 50:     }
 51:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');

 53:     cout << "\nFinallly, we push the value 5 onto q4, and then we "
 54:         "output the result of\ncomparing q3 and q4 using each of the "
 55:         "relational operators.";
 56:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 58:     q4.push(5);
 59:     cout << "\nq3 == q4 is " << boolalpha << (q3 == q4) << ".";
 60:     cout << "\nq3 != q4 is " << boolalpha << (q3 != q4) << ".";
 61:     cout << "\nq3 <  q4 is " << boolalpha << (q3 <  q4) << ".";
 62:     cout << "\nq3 <= q4 is " << boolalpha << (q3 <= q4) << ".";
 63:     cout << "\nq3 >  q4 is " << boolalpha << (q3 >  q4) << ".";
 64:     cout << "\nq3 >= q4 is " << boolalpha << (q3 >= q4) << ".";
 65:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 66: }