fork(3) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <map>
  5. #include <algorithm>
  6.  
  7.  
  8.  
  9. template <typename T>
  10. void unique_elements(std::vector<T>& vec)
  11. {
  12.  
  13.  
  14. std::map<T, int> m;
  15.  
  16. for(auto p=vec.begin(); p!=vec.end(); ++p)
  17. m[*p]++;
  18.  
  19. vec.clear();
  20.  
  21. for(auto p=m.begin(); p!=m.end(); ++p)
  22. if (p->second == 1) vec.push_back(p->first);
  23. }
  24.  
  25. int main()
  26. {
  27. using std::vector;
  28. using std::string;
  29.  
  30. vector<string> v{"b","a","x","y","z","a","a","z"};
  31.  
  32. unique_elements(v);
  33.  
  34. for(auto p=v.begin(); p!=v.end(); ++p)
  35. {
  36. std::cout << *p << std::endl;
  37. }
  38.  
  39. }
Success #stdin #stdout 0s 2968KB
stdin
Standard input is empty
stdout
b
x
y