Skip to content

Commit

Permalink
unordered_map for pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
susmitmishra125 committed Aug 1, 2021
1 parent e9b05be commit ac8df05
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions unordered_map_pair.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// https://www.geeksforgeeks.org/how-to-create-an-unordered_map-of-pairs-in-c/
// 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 kind
struct 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);
return hash1 ^ hash2;
}
};

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(2005, 3005);

// Inserting values in the unordered_map.
um[p1] = true;
um[p2] = false;
um[p3] = true;

cout << "Contents of the unordered_map : \n";
for (auto p : um)
cout << "[" << (p.first).first << ", "
<< (p.first).second << "] ==> "
<< p.second << "\n";

return 0;
}

0 comments on commit ac8df05

Please sign in to comment.