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

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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


My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.