Create another vector that contains a subrange of vector. - C++ STL

C++ examples for STL:vector

Description

Create another vector that contains a subrange of vector.

Demo Code

#include <iostream>
#include <vector>
using namespace std;
void show(const char *msg, vector<int> vect);
int main() {/*  w w w.  j a  v  a 2  s.  c  o  m*/
   vector<int> v(10);
   for(unsigned i=0; i < v.size(); ++i)
      v[i] = i*i;
   show("Contents of v: ", v);
   vector<int>::iterator itr;
   vector<int>::reverse_iterator ritr;
   vector<int> v2(v.begin()+2, v.end()-4);
   show("Contents of v2: ", v2);
   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