fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <string>
  5. #include <unordered_map>
  6.  
  7. int main()
  8. {
  9. std::vector<std::string> my_vec{ "hey","how","are","you","fine","and","you","fine" };
  10. std::unordered_map<std::string, int> m;
  11. for (auto it = my_vec.begin(); it != my_vec.end();)
  12. {
  13. if (++m[*it] == 1)
  14. ++it;
  15. else
  16. it = my_vec.erase(it);
  17. }
  18.  
  19. for (auto const& s : my_vec)
  20. {
  21. std::cout << s << ':' << m[s] << '\n';
  22. }
  23. }
  24.  
Success #stdin #stdout 0s 4460KB
stdin
Standard input is empty
stdout
hey:1
how:1
are:1
you:2
fine:2
and:1