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