fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <map>
  4.  
  5. struct Foo {
  6. Foo(std::string s) : foo_value(s) {}
  7. std::string foo_value;
  8. };
  9.  
  10. int main() {
  11. auto comp = [](const Foo& f1, const Foo& f2) { return f1.foo_value < f2.foo_value; };
  12. std::map<Foo, int, decltype(comp)> m({ {Foo("b"), 2}, {Foo("a"), 1} }, comp);
  13.  
  14. for (auto const &kv : m)
  15. std::cout << kv.first.foo_value << ": " << kv.second << std::endl;
  16.  
  17. return 0;
  18. }
Success #stdin #stdout 0s 4260KB
stdin
Standard input is empty
stdout
a: 1
b: 2