C++ examples for STL Algorithm:reverse
Reverse element in a sequence with algorithm reverse
#include <iostream> #include <algorithm> // algorithm definitions #include <vector> // vector class-template definition #include <iterator> // ostream_iterator using namespace std; int main() /*w ww . ja v a 2 s. c o m*/ { const int SIZE = 5; int a1[ SIZE ] = { 1, 3, 5, 7, 9 }; int a2[ SIZE ] = { 2, 4, 5, 7, 9 }; vector< int > v1( a1, a1 + SIZE ); // copy of a1 vector< int > v2( a2, a2 + SIZE ); // copy of a2 ostream_iterator< int > output( cout, " " ); cout << "Vector v1 contains: "; copy( v1.begin(), v1.end(), output ); // display vector output cout << "\nVector v2 contains: "; copy( v2.begin(), v2.end(), output ); // display vector output vector< int > results( v1.size() ); // place elements of v1 into results in reverse order cout << "\n\nVector v1 after reverse: "; reverse( v1.begin(), v1.end() ); // reverse elements of v1 copy( v1.begin(), v1.end(), output ); cout << endl; }