Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) std::reverse

 

std::reverse is an STL algorithm to reverse the contents of a container.

 

#include <algorithm>
#include <iterator>
#include <iostream>
#include <vector>

int main()
{
  std::vector<int> v;
  v.push_back(0);
  v.push_back(1);
  v.push_back(2);
  v.push_back(3);

  //Show the std::vector
  std::copy(v.begin(),v.end(),
    std::ostream_iterator<int>(std::cout," "));
  std::cout << '\n';

  //Reverse the std::vector
  std::reverse(v.begin(),v.end());

  //Show the std::vector
  std::copy(v.begin(),v.end(),
    std::ostream_iterator<int>(std::cout," "));
  std::cout << '\n';
}

 

Screen output:

 

0 1 2 3
3 2 1 0

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict