Copy elements in one vector into another vector in reverse order : reverse_copy « STL Algorithms Modifying sequence operations « C++






Copy elements in one vector into another vector in reverse order

 
 

#include <iostream>
using std::cout;
using std::endl;

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

int main()
{
   int a1[ 10 ] = { 1, 3, 5, 7, 9, 1, 3, 5, 7, 9 };
   std::vector< int > v1( a1, a1 + 10 ); // copy of a
   std::ostream_iterator< int > output( cout, " " );

   cout << "Vector v1 contains: ";
   std::copy( v1.begin(), v1.end(), output );

   std::vector< int > results2;

   std::reverse_copy( v1.begin(), v1.end(), std::back_inserter( results2 ) );
   cout << "\nAfter reverse_copy, results2 contains: ";
   std::copy( results2.begin(), results2.end(), output );
   cout << endl;

   return 0;
}

 /* 
Vector v1 contains: 1 3 5 7 9 1 3 5 7 9
After reverse_copy, results2 contains: 9 7 5 3 1 9 7 5 3 1

 */       
  








Related examples in the same category

1.Using reverse_copy, a copying version of the generic reverse algorithm
2.Use reverse_copy to print all of them in reverse order