Demonstrate push_back(), operator[], size() on vector - C++ STL

C++ examples for STL:vector

Description

Demonstrate push_back(), operator[], size() on vector

Demo Code

#include <iostream>
#include <vector>
using namespace std;
int main()//from  w w  w.  ja  v  a  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;
}

Result


Related Tutorials