Merge first half of a sequence with second half of another sequence using algorithm inplace_merge - C++ STL Algorithm

C++ examples for STL Algorithm:inplace_merge

Description

Merge first half of a sequence with second half of another sequence using algorithm inplace_merge

Demo Code

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

int main() /*ww w  . ja va  2  s.  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 ); 

}

Result


Related Tutorials