unordered_map is used to implement hash tables. It stores key value pairs. For every key, a hash function is computed and value is stored at that hash entry. Hash functions for standard data types (int, char, string, ..) are predefined. How to use our own data types for implementing hash tables?
unordered_map allows a third parameter which is used to specify our own hash function.
// Create an unordered_map with given KeyType, // ValueType and hash function defined by // MyHashType unordered_map<KeyType, ValueType, MyHashType> um;
Here MyHashFunction is class or struct that must contain an operator function ().
We must also implement operator == in our own class which is used for handling collisions.
Below is a sample code where objects of Person class are used as keys. We define our own hash function that uses sum of lengths of first and last names as key in the hash table. Note that the purpose of this code is to only demonstrate working with a simple code and sum of lengths may not be a good idea as a hash function.
// CPP program to demonstrate working of unordered_map // for user defined data types. #include <bits/stdc++.h> using namespace std; // Objects of this class are used as key in hash // table. This class must implement operator ==() // to handle collisions. struct Person { string first, last; // First and last names Person(string f, string l) { first = f; last = l; } // Match both first and last names in case // of collisions. bool operator==(const Person& p) const { return first == p.first && last == p.last; } }; class MyHashFunction { public: // Use sum of lengths of first and last names // as hash function. size_t operator()(const Person& p) const { return p.first.length() + p.last.length(); } }; // Driver code int main() { unordered_map<Person, int, MyHashFunction> um; Person p1("kartik", "kapoor"); Person p2("Ram", "Singh"); Person p3("Laxman", "Prasad"); um[p1] = 100; um[p2] = 200; um[p3] = 100; for (auto e : um) { cout << "[" << e.first.first << ", " << e.first.last << "] = > " << e.second << '\n'; } return 0; } |
[Laxman, Prasad] = > 100 [kartik, kapoor] = > 100 [Ram, Singh] = > 200
Another example where predefined operator functions of predefined hash class to make our overall hash.
// CPP program to demonstrate working of unordered_map // for user defined data types. #include <bits/stdc++.h> using namespace std; struct Person { string first, last; Person(string f, string l) { first = f; last = l; } bool operator==(const Person& p) const { return first == p.first && last == p.last; } }; class MyHashFunction { public: // We use predfined hash functions of strings // and define our hash function as XOR of the // hash values. size_t operator()(const Person& p) const { return (hash<string>()(p.first)) ^ (hash<string>()(p.last)); } }; // Driver code int main() { unordered_map<Person, int, MyHashFunction> um; Person p1("kartik", "kapoor"); Person p2("Ram", "Singh"); Person p3("Laxman", "Prasad"); um[p1] = 100; um[p2] = 200; um[p3] = 100; for (auto e : um) { cout << "[" << e.first.first << ", " << e.first.last << "] = > " << e.second << '\n'; } return 0; } |
[Laxman, Prasad] = > 100 [kartik, kapoor] = > 100 [Ram, Singh] = > 200
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- How to create an unordered_set of user defined class or struct in C++?
- How to create a Java HashMap of user defined class type?
- How to create a user defined javap tool?
- Function Interposition in C with an example of user defined malloc()
- User Defined Literals in C++
- 2D vector in C++ with user defined size
- Multi-set for user defined data type
- User defined Data Types in C++
- How to implement user defined Shared Pointers in C++
- How to Create a New User in Windows 10?
- How to find out where a function is defined using PHP ?
- Why error "$ is not defined" occurred in jQuery ?
- How to detect which one of the defined font was used in a web page?
- How to create a dynamic 2D array inside a class in C++ ?
- How to create a CSS rule or class at runtime using jQuery ?
- Program to create Custom Vector Class in C++
- How to create a custom String class in C++ with basic functionalities
- How to convert a class to another class type in C++?
- How to override the CSS properties of a class using another CSS class ?
- Difference between Base class and Derived class in C++
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

