Source of deque12.cpp


  1: //deque12.cpp

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

  8: int main()
  9: {
 10:     cout << "\nThis program illustrates the use of member functions "
 11:         "clear(),\nerase(), pop_back() and pop_front() for deque "
 12:         "objects.";
 13:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 15:     int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
 16:     deque<int> d(a, a+10);
 17:     cout << "\nFor d we have ...";
 18:     cout << "\nSize = " << d.size();
 19:     cout << "\nContents: ";
 20:     for(deque<int>::size_type i=0; i<d.size(); i++)
 21:         cout << d.at(i) << " ";
 22:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 24:     cout << "\nFirst, we delete the third component of d using this "
 25:         "call to erase():\np = d.erase(d.begin()+2);"
 26:         "\nand then display the remaining values in d.";
 27:     deque<int>::iterator p = d.erase(d.begin()+2);
 28:     cout << "\nSize = " << d.size();
 29:     cout << "\nContents: ";
 30:     for(deque<int>::size_type i=0; i<d.size(); i++)
 31:         cout << d.at(i) << " ";
 32:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 33:     cout << "\nThe iterator returned by the above call to erase() "
 34:         "points to " << *p << ".\n";
 35:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');

 37:     cout << "\nNext, we delete the 3rd through 7th components of d using "
 38:         "this call to erase():\np = d.erase(d.begin()+2, d.begin()+7);"
 39:         "\nand then display the remaining values in d.";
 40:     p = d.erase(d.begin()+2, d.begin()+7);
 41:     cout << "\nSize = " << d.size();
 42:     cout << "\nContents: ";
 43:     for(deque<int>::size_type i=0; i<d.size(); i++)
 44:         cout << d.at(i) << " ";
 45:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 46:     cout << "\nThe iterator returned by the above call to erase() "
 47:         "points to " << *p << ".\n";
 48:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');

 50:     cout << "\nNow we remove a component from the end of d (using "
 51:         "d.pop_back()),\nand display again to confirm.";
 52:     d.pop_back();
 53:     cout << "\nSize = " << d.size();
 54:     cout << "\nContents: ";
 55:     for(deque<int>::size_type i=0; i<d.size(); i++)
 56:         cout << d.at(i) << " ";
 57:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 59:     cout << "\nThen we remove a component from the front of d (using "
 60:         "d.pop_front()),\nand display again to confirm.";
 61:     d.pop_front();
 62:     cout << "\nSize = " << d.size();
 63:     cout << "\nContents: ";
 64:     for(deque<int>::size_type i=0; i<d.size(); i++)
 65:         cout << d.at(i) << " ";
 66:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 68:     cout << "\nFinally, we clear d and display once again to confirm:";
 69:     d.clear();
 70:     cout << "\nSize = " << d.size();
 71:     cout << "\nContents: ";
 72:     for(deque<int>::size_type i=0; i<d.size(); i++)
 73:         cout << d.at(i) << " ";
 74:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 75: }