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
|
#include <iostream>
#include <algorithm>
#include <set>
using namespace std;
template <typename T> T *noDuplicates( T arr[], int size, int &resultSize )
{
set<T> S;
T *temp = new T[size];
resultSize = copy_if( arr, arr + size, temp, [&S]( T val ){ return S.insert( val ).second; } ) - temp;
T *result = new T[resultSize];
copy( temp, temp + resultSize, result );
delete [] temp;
return result;
}
int main()
{
char a[] = { 'a', 's', 'h', 'b', 'r', 'o', 'a', 's', 'b', 'b', 'c', 'd', 'e', 'f' };
int asize = sizeof( a ) / sizeof( a[0] ), bsize;
char *b = noDuplicates( a, asize, bsize );
for ( int i = 0; i < asize; i++ ) cout << a[i] << " ";
cout << endl;
for ( int i = 0; i < bsize; i++ ) cout << b[i] << " ";
}
|