Using the STL generic reverse algorithm with a vector : reverse « STL Algorithms Modifying sequence operations « C++






Using the STL generic reverse algorithm with a vector

  
 

#include <iostream>
#include <vector>
#include <cassert>
#include <algorithm> // For reverse
using namespace std;
template <typename Container>

Container make(const char s[])
{
  return Container(&s[0], &s[strlen(s)]);
}

int main()
{
  vector<char> vector1 = make< vector<char> >("abc");
  reverse(vector1.begin(), vector1.end());
  assert (vector1 == make< vector<char> >("cba"));
  return 0;
}

        
    
  








Related examples in the same category

1.Use reverse to reverse the order of elements
2.Use reverse to reverse order from second to last element but one
3.Reverse the order of the found element with value 3 and all following elements
4.Reverse a sequence
5.Sort and reverse an int array