fork download
  1. #include <map>
  2. #include <string>
  3. #include <tuple>
  4. #include <iostream>
  5.  
  6. struct myStruct { int x, y; };
  7. using Key = std::pair<std::string, std::string>;
  8.  
  9. namespace something
  10. {
  11. struct Compare
  12. {
  13. bool operator()(const Key& lhs, const Key& rhs) const
  14. {
  15. // do the required comparison here
  16. return std::tie(lhs.first, lhs.second) < std::tie(rhs.first, rhs.second);
  17. }
  18. };
  19. }
  20. using myMap = std::map<Key, myStruct, something::Compare>;
  21.  
  22. int main()
  23. {
  24. myMap mp =
  25. {
  26. { { "name1", "name2" },{ 3,4 } },
  27. { { "aame1", "name2" },{ 8,4 } },
  28. { std::make_pair("fame1", "name2"),{ 2,4 } }, // or make pair
  29. { std::make_pair("fame1", "bame2"),{ 1,2 } }
  30. };
  31.  
  32. for(const auto& it: mp)
  33. {
  34. std::cout << it.first.first << " " << it.first.second << " "
  35. << it.second.x << " " << it.second.y << std::endl;
  36. }
  37.  
  38. return 0;
  39. }
  40.  
Success #stdin #stdout 0s 4544KB
stdin
Standard input is empty
stdout
aame1 name2 8 4
fame1 bame2 1 2
fame1 name2 2 4
name1 name2 3 4