Source of vector02.cpp


  1: //vector02.cpp

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

  8: int main()
  9: {
 10:     cout << "\nThis program illustrates the difference between "
 11:         "using v[i] and using v.a(i)\nfor access to the "
 12:         "components of a vector v.";
 13:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 15:     //Create a vector of five integer values
 16:     int a[] = {1, 2, 3, 4, 5};
 17:     vector<int> v(a, a+5);

 19:     cout << "\nContents of v using v[i]: ";
 20:     for (vector<int>::size_type i=0; i<v.size(); i++)
 21:         cout << v[i] << " ";
 22:     cout << endl;
 23:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');

 25:     cout << "\nContents of v using v.at(i): ";
 26:     for (vector<int>::size_type i=0; i<v.size(); i++)
 27:         cout << v.at(i) << " ";
 28:     cout << endl;
 29:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');

 31:     cout << "\nNow let's look at the value of v[6] (if we can).\n";
 32:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');
 33:     cout << "Value of v[6]: " << v[6] << endl;
 34:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');

 36:     cout << "\nNow let's look at the value of v.at(6) (if we can).\n";
 37:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');
 38:     cout << "Value of v.at(6): " << v.at(6) << endl;
 39:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');
 40: }