fork download
  1. #include <numeric>
  2. #include <vector>
  3. #include <set>
  4. #include <iostream>
  5. struct IndirectCompare {
  6. bool operator()(int* p1, int* p2) const { return *p1 < *p2; }
  7. };
  8. std::vector<int> uniquefy(std::vector<int> v)
  9. {
  10. // build a set of pointers, using custom comparer
  11. std::set<int*, IndirectCompare> s;
  12. for(auto i = v.begin(); i!=v.end(); ++i) // for(int& n : v)
  13. while(!s.insert(&*i).second)
  14. *i = rand() % 100;
  15. return v;
  16. }
  17. int main()
  18. {
  19. std::vector<int> in = {1,12,8,1,7,15,20,9,12};
  20. for(size_t n = 0; n != in.size(); ++n)
  21. std::cout << in[n] << ' ';
  22. std::cout << '\n';
  23. std::vector<int> out = uniquefy(in);
  24. for(size_t n = 0; n!= out.size(); ++n) // for(int n : out)
  25. std::cout << out[n] << ' ';
  26. std::cout << '\n';
  27. }
  28.  
Success #stdin #stdout 0s 2964KB
stdin
Standard input is empty
stdout
1 12 8 1 7 15 20 9 12 
1 12 8 83 7 15 20 9 86