Eliminate duplicate values from a sequence with algorithm unique - C++ STL Algorithm

C++ examples for STL Algorithm:unique

Description

Eliminate duplicate values from a sequence with algorithm unique

Demo Code

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

int main()//  ww w.j  a va  2  s. com
{
  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());

  // eliminate duplicate values from results2 
  vector< int >::iterator endLocation;
  endLocation = unique(results.begin(), results.end());

  cout << "\n\nAfter unique results2 contains:\n";
  copy(results.begin(), endLocation, output);

}

Result


Related Tutorials