Source of queue01.cpp


  1: //queue01.cpp

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

  7: int main()
  8: {
  9:     cout << "\nThis program illustrates the \"FIFO\" (First In, "
 10:         "First Out) behavior\nof a simple queue of characters, "
 11:         "as well as its default constructor,\nits copy constructor, "
 12:         "and the queue push(), pop(), front(), back(),\nempty(), "
 13:         "and size() member functions.";
 14:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 16:     queue<char> q1;
 17:     q1.push('A');
 18:     q1.push('B');
 19:     q1.push('C');
 20:     q1.push('D');
 21:     cout << "\nThe queue q1 contains " << q1.size() << " values.";
 22:     cout << "\nThe front value is " << q1.front() << " and the back "
 23:         "value is " << q1.back() << ".";
 24:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 26:     queue<char> q2(q1);
 27:     q2.push('E');
 28:     cout << "\nThe queue q2 is created as a copy of q1, after which "
 29:         "another value is added,\nso its size is " << q2.size() << ".";
 30:     cout << "\nThe front value is " << q2.front() << " and the back "
 31:         "value is " << q2.back() << ".";
 32:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

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

 44:     cout << "\nHere 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');
 52: }