Reverse a sequence with reverse() - C++ STL Algorithm

C++ examples for STL Algorithm:reverse

Description

Reverse a sequence with reverse()

Demo Code

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void show(const char *msg, vector<int> vect);
int main()// ww w .  j  a v a  2s. c  om
{
   vector<int> v;
   for(int i=0; i<10; i++)
      v.push_back(i);
   show("Original order: ", v);
   cout << endl;
   // Reverse v.
   reverse(v.begin(), v.end());
   show("After reversal: ", v);
   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