How to create an unordered_map of pairs in C++?

24 Mar, 2022
Unordered Map does not contain a hash function for a pair like it has for int, string, etc, So if we want to hash a pair then we have to explicitly provide it with a hash function that can hash a pair. unordered_map can takes upto 5 arguments:  
Key : Type of key values
Value : Type of value to be stored against the key
Hash Function : A function which is used to hash the given key. If not provided it uses default hash function.
Pred : A function which is used so that no two keys can have same hash values
Alloc : An object used to define the memory model for the map
hash_function can be anything, given that it can hash the given key.Prerequisite : How to create an unordered_map of user defined class? 
CPP
// CPP program to demonstrate implementation of// unordered_map for a pair.#include <bits/stdc++.h>using namespace std; // A hash function used to hash a pair of any kindstruct hash_pair {    template <class T1, class T2>    size_t operator()(const pair<T1, T2>& p) const    {        auto hash1 = hash<T1>{}(p.first);        auto hash2 = hash<T2>{}(p.second);         if (hash1 != hash2) {            return hash1 ^ hash2;                     }                 // If hash1 == hash2, their XOR is zero.          return hash1;    }}; int main(){    // Sending the hash function as a third argument    unordered_map<pair<int, int>, bool, hash_pair> um;     // Creating some pairs to be used as keys    pair<int, int> p1(1000, 2000);    pair<int, int> p2(2000, 3000);    pair<int, int> p3(3005, 3005);    pair<int, int> p4(4000, 4000);     // Inserting values in the unordered_map.    um[p1] = true;    um[p2] = false;    um[p3] = true;    um[p4] = false;     cout << "Contents of the unordered_map : \n";    for (auto p : um)        cout << "[" << (p.first).first << ", "             << (p.first).second << "] ==> " << p.second             << "\n";     return 0;}
Contents of the unordered_map : 
[4000, 4000] ==> 0
[3005, 3005] ==> 1
[1000, 2000] ==> 1
[2000, 3000] ==> 0

Note : We can create map for a pair. Check out map of pair. The reason is, map is based on self balancing BSTs and does not require a hash function.Exercise Problem : Nikhil is a travelling salesman and today he is visiting houses in a new locality to sell encyclopedias. The new city is in the from of a grid of x*y(1<=x<=10^9, 1<=y<=10^9) and at every intersection there is a house. Now he is not very good with remembering the houses that he has already visited, so whenever he goes into a house he tells you the coordinate of the house. Your job is to remember the coordinate and at the end of the day tell him all the houses that he visited on that day.Examples:  
Input : Enter the number of houses that he visited today :5 Enter the coordinate of HouseNo. 1 :1000 12985 Enter the coordinate of HouseNo. 2 :12548 25621 Enter the coordinate of HouseNo. 3 :14586 26481 Enter the coordinate of HouseNo. 4 :12 63 Enter the coordinate of HouseNo. 5 :14689 36945 Output : Houses that he visited today: 12 63 14689 36945 14586 26481 1000 12985 12548 25621
 
joshuahernandez
cpp-pair
cpp-unordered_map
Picked
STL
Technical Scripter 2018
C++
Technical Scripter
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Sorting a vector in C++
Polymorphism in C++
Pair in C++ Standard Template Library (STL)
Friend class and function in C++
std::string class in C++
Queue in C++ Standard Template Library (STL)
std::find in C++
Unordered Sets in C++ Standard Template Library
List in C++ Standard Template Library (STL)
vector insert() function in C++ STL