Copy elements of one sequence into another sequence in reverse order with algorithm reverse_copy - C++ STL Algorithm

C++ examples for STL Algorithm:reverse_copy

Description

Copy elements of one sequence into another sequence in reverse order with algorithm reverse_copy

Demo Code

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

int main() //from  w  ww .j a v a  2s  . c  o m
{ 
    const int SIZE = 10; 
    int a1[ SIZE ] = { 1, 3, 5, 7, 9, 1, 3, 5, 7, 9 }; 
    vector< int > v1( a1, a1 + SIZE ); // copy of a 
    ostream_iterator< int > output( cout, " " ); 

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

    // merge first half of v1 with second half of v1 such that 
    // v1 contains sorted set of elements after merge 
    inplace_merge( v1.begin(), v1.begin() + 5, v1.end() ); 

    cout << "\nAfter inplace_merge, v1 contains: "; 
    copy( v1.begin(), v1.end(), output ); 

    vector< int > results2; 

    // copy elements of v1 into results2 in reverse order 
    reverse_copy( v1.begin(), v1.end(), back_inserter( results2 ) ); 
    cout << "\nAfter reverse_copy, results2 contains: "; 
    copy( results2.begin(), results2.end(), output ); 
    cout << endl; 
}

Result


Related Tutorials