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
#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 :
size_t operator()( const Person& p) const
{
return p.first.length() + p.last.length();
}
};
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
#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 :
size_t operator()( const Person& p) const
{
return (hash<string>()(p.first)) ^
(hash<string>()(p.last));
}
};
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