VOOZH about

URL: https://www.geeksforgeeks.org/cpp/how-to-create-an-unordered_map-of-user-defined-class-in-cpp/

⇱ How to create an unordered_map of user defined class in C++? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to create an unordered_map of user defined class in C++?

Last Updated : 11 Jul, 2025

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.
 


Output: 
[Laxman, Prasad] = > 100
[kartik, kapoor] = > 100
[Ram, Singh] = > 200

 

Another example where predefined operator functions of predefined hash class to make our overall hash.
 


Output: 
[Laxman, Prasad] = > 100
[kartik, kapoor] = > 100
[Ram, Singh] = > 200

 
Comment
Article Tags:
Article Tags: