Copy only unique elements of one sequence into another sequence using algorithm unique_copy - C++ STL Algorithm

C++ examples for STL Algorithm:unique_copy

Description

Copy only unique elements of one sequence into another sequence using algorithm unique_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() //  w ww. j a  va2 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 ); 

    vector< int > results1; 

    // copy only unique elements of v1 into results1 
    unique_copy( v1.begin(), v1.end(), back_inserter( results1 ) ); 
    cout << "\nAfter unique_copy results1 contains: "; 
    copy( results1.begin(), results1.end(), output ); 

}

Result


Related Tutorials