Skip to content
Related Articles

Related Articles

How to create an unordered_map of user defined class in C++?
  • Last Updated : 09 Dec, 2018

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;
}
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.




// 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;
}
Output:
[Laxman, Prasad] = > 100
[kartik, kapoor] = > 100
[Ram, Singh] = > 200
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :