Use std::unique to eliminate duplicate values : unique « STL Algorithms Modifying sequence operations « C++






Use std::unique to eliminate duplicate values

 
 

#include <iostream>
using std::cout;
using std::endl;

#include <algorithm>
#include <vector>
#include <iterator>

int main()
{
   int a1[ 5 ] = { 1, 1, 1, 7, 9 };

   std::vector< int > v1( a1, a1 + 5 );

   std::ostream_iterator< int > output( cout, " " );

   std::copy( v1.begin(), v1.end(), output ); // display vector output

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

   cout << endl;
   std::copy( v1.begin(), endLocation, output );

   return 0;
}

/* 
1 1 1 7 9
1 7 9
 */        
  








Related examples in the same category

1.Use unique to remove consecutive duplicates
2.Combine unique and erase to remove elements if there was a previous greater element