Source of vector13.cpp


  1: //vector13.cpp

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

  8: int main()
  9: {
 10:     cout << "\nThis program illustrates the member functions\n"
 11:         "reserve() and resize() for vector objects.";
 12:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 14:     vector<int> v1;
 15:     cout << "\nTo begin we have, for v1:";
 16:     cout << "\nSize = " << v1.size() << "  Capacity = " << v1.capacity();
 17:     cout << "\nContents: ";
 18:     for(vector<int>::size_type i=0; i<v1.size(); i++)
 19:         cout << v1.at(i) << " ";
 20:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 22:     v1.reserve(6);
 23:     cout << "\nAfter v1.reserve(6) we have, for v1:";
 24:     cout << "\nSize = " << v1.size() << "  Capacity = " << v1.capacity();
 25:     cout << "\nContents: ";
 26:     for(vector<int>::size_type i=0; i<v1.size(); i++)
 27:         cout << v1.at(i) << " ";
 28:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 30:     v1.resize(4);
 31:     cout << "\nAfter v1.resize(4) we have, for v1:";
 32:     cout << "\nSize = " << v1.size() << "  Capacity = " << v1.capacity();
 33:     cout << "\nContents: ";
 34:     for(vector<int>::size_type i=0; i<v1.size(); i++)
 35:         cout << v1.at(i) << " ";
 36:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 38:     vector<int> v2(10, 3);
 39:     cout << "\nTo begin we have, for v2:";
 40:     cout << "\nSize = " << v2.size() << "  Capacity = " << v2.capacity();
 41:     cout << "\nContents: ";
 42:     for(vector<int>::size_type i=0; i<v2.size(); i++)
 43:         cout << v2.at(i) << " ";
 44:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 46:     v2.reserve(6);
 47:     cout << "\nAfter v2.reserve(6) we have, for v2:";
 48:     cout << "\nSize = " << v2.size() << "  Capacity = " << v2.capacity();
 49:     cout << "\nContents: ";
 50:     for(vector<int>::size_type i=0; i<v2.size(); i++)
 51:         cout << v2.at(i) << " ";
 52:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 54:     v2.reserve(16);
 55:     cout << "\nAfter v2.reserve(16) we have, for v2:";
 56:     cout << "\nSize = " << v2.size() << "  Capacity = " << v2.capacity();
 57:     cout << "\nContents: ";
 58:     for(vector<int>::size_type i=0; i<v2.size(); i++)
 59:         cout << v2.at(i) << " ";
 60:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 62:     v2.resize(8);
 63:     cout << "\nAfter v2.resize(8) we have, for v2:";
 64:     cout << "\nSize = " << v2.size() << "  Capacity = " << v2.capacity();
 65:     cout << "\nContents: ";
 66:     for(vector<int>::size_type i=0; i<v2.size(); i++)
 67:         cout << v2.at(i) << " ";
 68:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 70:     v2.resize(12);
 71:     cout << "\nAfter v2.resize(12) we have, for v2:";
 72:     cout << "\nSize = " << v2.size() << "  Capacity = " << v2.capacity();
 73:     cout << "\nContents: ";
 74:     for(vector<int>::size_type i=0; i<v2.size(); i++)
 75:         cout << v2.at(i) << " ";
 76:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 78:     v2.resize(6, -1);
 79:     cout << "\nAfter v2.resize(6, -1) we have, for v2:";
 80:     cout << "\nSize = " << v2.size() << "  Capacity = " << v2.capacity();
 81:     cout << "\nContents: ";
 82:     for(vector<int>::size_type i=0; i<v2.size(); i++)
 83:         cout << v2.at(i) << " ";
 84:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 86:     v2.resize(10, -1);
 87:     cout << "\nAfter v2.resize(10, -1) we have, for v2:";
 88:     cout << "\nSize = " << v2.size() << "  Capacity = " << v2.capacity();
 89:     cout << "\nContents: ";
 90:     for(vector<int>::size_type i=0; i<v2.size(); i++)
 91:         cout << v2.at(i) << " ";
 92:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 94:     v2.resize(20, -2);
 95:     cout << "\nAfter v2.resize(20, -2) we have, for v2:";
 96:     cout << "\nSize = " << v2.size() << "  Capacity = " << v2.capacity();
 97:     cout << "\nContents: ";
 98:     for(vector<int>::size_type i=0; i<v2.size(); i++)
 99:         cout << v2.at(i) << " ";
100:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
101: }