fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. int main() {
  8. vector<string> elements{"a","a","b","b","c"};
  9. // "elements" is populated ....
  10. // ...
  11.  
  12. // Find duplicates:
  13. vector<string> uniqueElements;
  14.  
  15. for (int i = 0; i < elements.size(); ++i)
  16. {
  17. string name = elements[i];
  18. vector<string>::iterator it = find(uniqueElements.begin(), uniqueElements.end(), name);
  19.  
  20. if(it != uniqueElements.end()) // ERROR
  21. {
  22. cerr << "Duplicate element " << name << endl;
  23. }
  24. else
  25. uniqueElements.push_back(name);
  26. }
  27. for (const auto & e : uniqueElements)
  28. std::cout << e << std::endl;
  29. return 0;
  30. }
Success #stdin #stdout #stderr 0s 3276KB
stdin
Standard input is empty
stdout
a
b
c
stderr
Duplicate element a
Duplicate element b