Change vector element values using array subscript syntax - C++ STL

C++ examples for STL:vector

Description

Change vector element values using array subscript syntax

Demo Code

#include <iostream>
#include <vector>
using namespace std;
void show(const char *msg, vector<int> vect);
int main() {// ww w .j  a va2 s . co  m
   vector<int> v(10);
   for(unsigned i=0; i < v.size(); ++i)
      v[i] = i*i;
   show("Contents of v: ", v);
   // Create another vector that contains a subrange of v.
   vector<int> v2(v.begin()+2, v.end()-4);
   // Change the values of some of v2's elements.
   v2[1] = 100;
   v2[2] = 88;
   v2[4] = 99;
   show("After the assignments, v2 now contains: ", v2);
   cout << endl;
   return 0;
}
void show(const char *msg, vector<int> vect) {
   cout << msg;
   for(unsigned i=0; i < vect.size(); ++i)
      cout << vect[i] << " ";
   cout << "\n";
}

Result


Related Tutorials