Source of deque04.cpp


  1: //deque04.cpp

  3: #include <iostream>
  4: #include <string>
  5: #include <deque>
  6: using namespace std;

  8: int main()
  9: {
 10:     cout << "\nThis program illustrates push_back() and push_front() "
 11:         "for putting values into\na deque, and shows the size of the "
 12:         "deque after each call to either of these\nmember functions. "
 13:         "It is worth pointing out here that the deque container class\n"
 14:         "has the push_front() member function while the vector container "
 15:         "class does not,\nand the vector container class has a capacity() "
 16:         "member function while the deque\ncontainer class does not.";
 17:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 19:     int size;
 20:     cout << "\nEnter starting size for deque: ";
 21:     cin >> size;

 23:     //Create the deque
 24:     deque<int> d(size);

 26:     //Add values to back of deque, one at a time.
 27:     cout << "Enter number of values to add to back: ";
 28:     int numberToAddToBack;
 29:     cin >> numberToAddToBack;  cin.ignore(80, '\n');

 31:     cout << "\nWe will now add the values 1 to " << numberToAddToBack
 32:         << " to the back of the deque.";
 33:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 34:     for (int i=1; i<=numberToAddToBack; i++) d.push_back(i);
 35:     cout << "\nCurrent deque size: " << d.size();
 36:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 38:     //Add values to front of deque, one at a time.
 39:     cout << "\nEnter number of values to add to front: ";
 40:     int numberToAddToFront;
 41:     cin >> numberToAddToFront;  cin.ignore(80, '\n');

 43:     cout << "\nWe will now add the values 1 to " << numberToAddToFront
 44:         << " to the front of the deque.";
 45:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 46:     for (int i=1; i<=numberToAddToFront; i++) d.push_front(i);
 47:     cout << "\nCurrent deque size: " << d.size();
 48:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 50:     cout << "\nContents of the deque: ";
 51:     for (deque<double>::size_type i=0; i<d.size(); i++)
 52:         cout << d[i] << " ";
 53:     cout << endl;
 54:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');
 55: }