Create an empty vector and then assign it a sequence that is the reverse of another vector. - C++ STL

C++ examples for STL:vector

Description

Create an empty vector and then assign it a sequence that is the reverse of another 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 va  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);
   // Create an empty vector and then assign it a sequence
   // that is the reverse of v.
   vector<int> v3;
   v3.assign(v.rbegin(), v.rend());
   show("v3 contains the reverse of v: ", v3);
   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