1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
#include <iostream>
#include <list>
#include <set>
#include <algorithm>
using namespace std;
//================================================
template <typename T> void removeDuplicates( list<T> &L )
{
set<T> S;
L.erase( remove_if( L.begin(), L.end(), [&S]( T val ){ return !S.insert( val ).second; } ), L.end() );
}
//================================================
template <typename T> ostream & operator << ( ostream &strm, list<T> L ) { for ( auto e : L ) cout << e << " "; return strm; }
//================================================
int main()
{
list<char> L = { 'M', 'A', 'S', 'S', 'A', 'C', 'H', 'U', 'S', 'E', 'T', 'T', 'S' };
cout << "Before: " << L << endl;
removeDuplicates( L );
cout << "After: " << L << endl;
}
|