Source of vector11.cpp


  1: //vector11.cpp

  3: #include <iostream>
  4: #include <vector>
  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:     vector<int> v(a, a+10);
 14:     cout << "\nFor v we have ...";
 15:     cout << "\nSize = " << v.size() << "  Capacity = " << v.capacity();
 16:     cout << "\nContents: ";
 17:     for(vector<int>::size_type i=0; i<v.size(); i++)
 18:         cout << v.at(i) << " ";
 19:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 21:     vector<int>::iterator p = v.insert(v.begin()+3, 17);
 22:     cout << "\nNow we insert 17, using this call to insert():"
 23:         "\np = v.insert(v.begin()+3, 17);\nThen for v we have ...";
 24:     cout << "\nSize = " << v.size() << "  Capacity = " << v.capacity();
 25:     cout << "\nContents: ";
 26:     for(vector<int>::size_type i=0; i<v.size(); i++)
 27:         cout << v.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:     v.insert(v.begin()+9, 5, 33);
 35:     cout << "\nNext we insert 5 copies of 33, using this call to "
 36:         "insert():\nv.insert(v.begin()+9, 5, 33);\nThen for v we have ...";
 37:     cout << "\nSize = " << v.size() << "  Capacity = " << v.capacity();
 38:     cout << "\nContents: ";
 39:     for(vector<int>::size_type i=0; i<v.size(); i++)
 40:         cout << v.at(i) << " ";
 41:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

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

 53:     v.insert(v.begin()+7, v.begin()+1, v.begin()+4);
 54:     cout << "\nFinally we insert some values from the vector itself,\n"
 55:         "using this call to insert():"
 56:         "\nv.insert(v.begin()+7, v.begin()+1, v.begin()+4);"
 57:         "\nThen for v we have ...";
 58:     cout << "\nSize = " << v.size() << "  Capacity = " << v.capacity();
 59:     cout << "\nContents: ";
 60:     for(vector<int>::size_type i=0; i<v.size(); i++)
 61:         cout << v.at(i) << " ";
 62:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 63: }