fork(2) 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. return a.size() < b.size();
  8. }
  9. };
  10.  
  11. template<typename T>
  12. void print(const T& t){
  13. for(auto& it : t)
  14. std::cout << it << "\n";
  15. }
  16.  
  17. template<typename T>
  18. void insert(T& t, const char* value)
  19. {
  20. auto result = t.insert(value);
  21. std::cout << "After inserting " << value << ". inserted? " << result.second << "\n";
  22. print(t);
  23. std::cout << "\n";
  24. }
  25.  
  26. int main() {
  27. std::set<std::string, compare> c;
  28.  
  29. insert(c, "Apple");
  30. insert(c, "Lemon");
  31.  
  32. print(c);
  33.  
  34. return 0;
  35. }
  36.  
Success #stdin #stdout 0s 3276KB
stdin
Standard input is empty
stdout
After inserting Apple. inserted? 1
Apple

After inserting Lemon. inserted? 0
Apple

Apple