fork download
  1. #include <set>
  2. #include <string>
  3. #include <iostream>
  4.  
  5. struct Bar {
  6. Bar(std::string s) : str(s) {}
  7. std::string str;
  8. };
  9.  
  10. struct Foo {
  11. Foo(Bar* p) : pBar(p) {}
  12. Bar* pBar;
  13. };
  14.  
  15. int main() {
  16. auto comp = [](const Foo* f1, const Foo* f2) { return f1->pBar->str < f2->pBar->str; };
  17. std::set<Foo*, decltype(comp)> set_of_foos(comp);
  18.  
  19. set_of_foos.emplace(new Foo(new Bar("x")));
  20. set_of_foos.emplace(new Foo(new Bar("y")));
  21.  
  22. auto it = set_of_foos.find(new Foo(new Bar("x")));
  23. if (it == std::end(set_of_foos))
  24. std::cout << "Element not found!" << std::endl;
  25. else
  26. std::cout << "Element found: " << (*it)->pBar->str << std::endl;
  27.  
  28. return 0;
  29. }
Success #stdin #stdout 0s 4552KB
stdin
Standard input is empty
stdout
Element found: x