fork(1) download
  1. #include <iostream>
  2. #include <string>
  3. #include <set>
  4.  
  5. struct compare {
  6. bool operator() (const std::string& a, const std::string& b) const{
  7. if (a.size() < b.size())
  8. return true;
  9. if (a.size() == b.size() && a.compare(b) < 0)
  10. return true;
  11. return false;
  12. }
  13. };
  14.  
  15. template<typename T>
  16. void print(const T& t){
  17. for(auto& it : t)
  18. std::cout << it << "\n";
  19. }
  20.  
  21. template<typename T>
  22. void insert(T& t, const char* value)
  23. {
  24. auto result = t.insert(value);
  25. std::cout << "After inserting " << value << ". inserted? " << result.second << "\n";
  26. print(t);
  27. std::cout << "\n";
  28. }
  29.  
  30. int main() {
  31. std::set<std::string, compare> c;
  32.  
  33. insert(c, "Apple");
  34. insert(c, "Lemon");
  35. insert(c, "Fig");
  36. insert(c, "Kumquat");
  37.  
  38. print(c);
  39.  
  40. return 0;
  41. }
  42.  
Success #stdin #stdout 0s 3276KB
stdin
Standard input is empty
stdout
After inserting Apple. inserted? 1
Apple

After inserting Lemon. inserted? 1
Apple
Lemon

After inserting Fig. inserted? 1
Fig
Apple
Lemon

After inserting Kumquat. inserted? 1
Fig
Apple
Lemon
Kumquat

Fig
Apple
Lemon
Kumquat