C++ vector operator[]

Description

C++ vector operator[]

#include <iostream>
#include <vector>
using namespace std;
int main()//from w w  w. j a va 2 s .  c  o m
{
   vector<int> v;                 // create a vector of ints
   v.push_back(10);               // put values at end of array
   v.push_back(11);
   v.push_back(12);
   v.push_back(13);
   v[0] = 20;                     // replace with new values
   v[3] = 23;
   for(int j=0; j<v.size(); j++)  // display vector contents
      cout << v[j] << ' ';        // 20 11 12 23
   cout << endl;
   return 0;
}



PreviousNext

Related