fork download
  1. #include <set>
  2. #include <list>
  3. #include <string>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9.  
  10. // set prevents dupes, list preserves order
  11. set<string> theset;
  12. list<set<string>::iterator> thelist;
  13.  
  14. // insertion is like this:
  15. auto insert = [&] (const string &str) {
  16. auto inserted = theset.insert(str);
  17. if (inserted.second)
  18. thelist.push_back(inserted.first);
  19. };
  20.  
  21. // then, for example:
  22. insert("zebra"); // first zebra
  23. insert("chair a"); // first chair a
  24. insert("desk"); // first desk
  25. insert("desk");
  26. insert("chair b"); // first chair b
  27. insert("chair a");
  28. insert("chair a");
  29. insert("table"); // first table
  30. insert("chair a");
  31. insert("xylophone"); // first xylophone
  32. insert("zebra");
  33.  
  34. // access can be done like:
  35. for (auto istr : thelist)
  36. cout << *istr << endl;
  37.  
  38. }
Success #stdin #stdout 0.01s 5452KB
stdin
Standard input is empty
stdout
zebra
chair a
desk
chair b
table
xylophone