Copy element in a sequence to another sequence in reverse order using algorithm copy_backward - C++ STL Algorithm

C++ examples for STL Algorithm:copy_backward

Description

Copy element in a sequence to another sequence in reverse order using algorithm copy_backward

Demo Code

#include <iostream> 
#include <algorithm> // algorithm definitions 
#include <vector> // vector class-template definition 
#include <iterator> // ostream_iterator 
using namespace std; 

int main() //from  w ww  . j  av  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 
   copy_backward( v1 .begin(), v1.end(), results.end() ); 
   cout << "\n\nAfter copy_backward, results contains: "; 
   copy( results.begin(), results.end(), output ); 

}

Result


Related Tutorials