fork download
  1. #include <set>
  2. #include <string>
  3. #include <iostream>
  4.  
  5. struct test {
  6. std::string key;
  7. std::string data;
  8. };
  9.  
  10. auto comp = [](const test& t1, const test& t2) { return t1.key < t2.key; };
  11. std::set<test, decltype(comp)> s(comp);
  12.  
  13. int main() {
  14. test newmember;
  15. newmember.key = "key";
  16. newmember.data = "data";
  17. s.insert(newmember);
  18.  
  19. auto it = s.find(test{ "key", "" });
  20. std::cout << "(1) " << it->key << ", " << it->data << std::endl;
  21.  
  22. newmember.data = "otherdata";
  23. s.insert(newmember); // Insertion fails!
  24. for (auto const& t : s) {
  25. std::cout << "(2) " << t.key << ", " << t.data << std::endl;
  26. }
  27.  
  28. return 0;
  29. }
Success #stdin #stdout 0s 4192KB
stdin
Standard input is empty
stdout
(1) key, data
(2) key, data