Source of deque11.cpp


  1: //deque11.cpp

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

  7: int main()
  8: {
  9:     cout << "\nThis program illustrates the insert() member function.";
 10:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

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

 21:     deque<int>::iterator p = d.insert(d.begin()+3, 17);
 22:     cout << "\nNow we insert 17, using this call to insert():"
 23:         "\np = d.insert(d.begin()+3, 17);\nThen for d we have ...";
 24:     cout << "\nSize = " << d.size();
 25:     cout << "\nContents: ";
 26:     for(deque<int>::size_type i=0; i<d.size(); i++)
 27:         cout << d.at(i) << " ";
 28:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 30:     cout << "\nAnd the value pointed to by the iterator\nreturned "
 31:         "by this call to insert() is also " << *p << ".";
 32:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 34:     d.insert(d.begin()+9, 5, 33);
 35:     cout << "\nNext we insert 5 copies of 33, using this call to "
 36:         "insert():\nd.insert(d.begin()+9, 5, 33);\nThen for d we have ...";
 37:     cout << "\nSize = " << d.size();
 38:     cout << "\nContents: ";
 39:     for(deque<int>::size_type i=0; i<d.size(); i++)
 40:         cout << d.at(i) << " ";
 41:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 43:     d.insert(d.begin()+11, a+1, a+4);
 44:     cout << "\nNext we insert some values from an array, using this call "
 45:         "to insert():\nd.insert(d.begin()+11, a+1, a+4);"
 46:         "\nThen for d we have ...";
 47:     cout << "\nSize = " << d.size();
 48:     cout << "\nContents: ";
 49:     for(deque<int>::size_type i=0; i<d.size(); i++)
 50:         cout << d.at(i) << " ";
 51:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 53:     //The following code should work the same for a deque as it does
 54:     //for vectors (see vector11.cpp) but it does not. It causes a runtime
 55:     //error under Visual C++ 2005. With gnu C++ 3.3.3 under Debian it does
 56:     //not produce a runtime error, but it does not produce the expected
 57:     //result either.
 58:     //d.insert(d.begin()+7, d.begin()+1, d.begin()+4);
 59:     //cout << "\nFinally we insert some values from the deque itself,\n"
 60:     //    "using this call to insert():"
 61:     //    "\nd.insert(d.begin()+7, d.begin()+1, d.begin()+4);"
 62:     //    "\nThen for d we have ...";
 63:     //cout << "\nSize = " << d.size();
 64:     //cout << "\nContents: ";
 65:     //for(deque<int>::size_type i=0; i<d.size(); i++)
 66:     //    cout << d.at(i) << " ";
 67:     //cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 68: }