fork download
  1. #include <iostream>
  2. #include <unordered_map>
  3. #include <utility>
  4.  
  5. // Custom hash function for std::pair<int, int>
  6. struct PairHash {
  7. size_t operator()(const std::pair<int, int>& p) const {
  8. // Combine the hashes of the two integers
  9. size_t hash1 = std::hash<int>{}(p.first);
  10. size_t hash2 = std::hash<int>{}(p.second);
  11. // A simple way to combine the hashes is to XOR them
  12. return hash1 ^ (hash2 << 1); // Shifting hash2 to avoid collisions when both integers are the same
  13. }
  14. };
  15.  
  16. int main() {
  17. std::unordered_map<std::pair<int, int>, int, PairHash> myMap;
  18.  
  19. // Inserting values
  20. myMap[std::make_pair(1, 2)] = 5;
  21. myMap[std::make_pair(3, 4)] = 10;
  22.  
  23. // Accessing values
  24. std::cout << "Value of (1, 2): " << myMap[std::make_pair(1, 2)] << std::endl;
  25. std::cout << "Value of (3, 4): " << myMap[std::make_pair(3, 4)] << std::endl;
  26.  
  27. return 0;
  28. }
  29.  
Success #stdin #stdout 0s 5304KB
stdin
Standard input is empty
stdout
Value of (1, 2): 5
Value of (3, 4): 10