#include <numeric>
#include <vector>
#include <set>
#include <iostream>
struct IndirectCompare {
bool operator()(int* p1, int* p2) const { return *p1 < *p2; }
};
std::vector<int> uniquefy(std::vector<int> v)
{
// build a set of pointers, using custom comparer
std::set<int*, IndirectCompare> s;
for(auto i = v.begin(); i!=v.end(); ++i) // for(int& n : v)
while(!s.insert(&*i).second)
*i = rand() % 100;
return v;
}
int main()
{
std::vector<int> in = {1,12,8,1,7,15,20,9,12};
for(size_t n = 0; n != in.size(); ++n)
std::cout << in[n] << ' ';
std::cout << '\n';
std::vector<int> out = uniquefy(in);
for(size_t n = 0; n!= out.size(); ++n) // for(int n : out)
std::cout << out[n] << ' ';
std::cout << '\n';
}
I2luY2x1ZGUgPG51bWVyaWM+CiNpbmNsdWRlIDx2ZWN0b3I+CiNpbmNsdWRlIDxzZXQ+CiNpbmNsdWRlIDxpb3N0cmVhbT4Kc3RydWN0IEluZGlyZWN0Q29tcGFyZSB7CiAgICBib29sIG9wZXJhdG9yKCkoaW50KiBwMSwgaW50KiBwMikgY29uc3QgeyByZXR1cm4gKnAxIDwgKnAyOyB9Cn07CnN0ZDo6dmVjdG9yPGludD4gdW5pcXVlZnkoc3RkOjp2ZWN0b3I8aW50PiB2KQp7CiAgICAvLyBidWlsZCBhIHNldCBvZiBwb2ludGVycywgdXNpbmcgY3VzdG9tIGNvbXBhcmVyCiAgICBzdGQ6OnNldDxpbnQqLCBJbmRpcmVjdENvbXBhcmU+IHM7CiAgICBmb3IoYXV0byBpID0gdi5iZWdpbigpOyBpIT12LmVuZCgpOyArK2kpIC8vIGZvcihpbnQmIG4gOiB2KQogICAgICAgIHdoaWxlKCFzLmluc2VydCgmKmkpLnNlY29uZCkKICAgICAgICAgICAgKmkgPSByYW5kKCkgJSAxMDA7CiAgICByZXR1cm4gdjsKfQppbnQgbWFpbigpCnsKICAgc3RkOjp2ZWN0b3I8aW50PiBpbiA9IHsxLDEyLDgsMSw3LDE1LDIwLDksMTJ9OwogICBmb3Ioc2l6ZV90IG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