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






#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








24.25.unique
24.25.1.Use std::unique to eliminate duplicate values
24.25.2.Use unique to remove consecutive duplicates
24.25.3.Combine unique and erase to remove elements if there was a previous greater element