In C++, std::unordered_map::hash_function() returns a copy of the hash function object the container uses.
A hash function turns a key into a number. The unordered_map then picks a bucket from that number. The default is std::hash<Key>. Include <unordered_map> to use it.
This is a member of std::unordered_map<Key, T>, a hash table of key and value pairs. Each key is unique. Find, insert, and erase are usually average constant time. Unlike std::map, keys are not sorted and iteration order is unspecified. Read a value with at() or find(). The m[key] operator also works, but it adds a default value when the key is missing.
Syntax
Call hash_function, then call the returned object on a key.
C++
auto h = m.hash_function();
std::size_t code = h("Raj");
std::size_t same = m.hash_function()("Raj");
Call
What it does
m.hash_function()
Returns the hasher object stored in the unordered_map.
m.hash_function()(key)
Hashes key and returns a std::size_t code.
Equal keys must produce equal hash codes. Different keys should usually produce different codes, but collisions are allowed and handled by the table.
Library declarations
C++
hasher hash_function() const;
Parameters
Parameter
Description
None
hash_function() takes no parameters. Call it with empty brackets.
Return Value
Return Value
Description
hasher
A copy of the hash function object. Often std::hash<Key>.
C++ Examples
Example 1 — Call the Hash Function
Get the hasher and hash the same string twice.
C++
#include <iostream>
#include <string>
#include <unordered_map>
int main() {
std::unordered_map<std::string, int> m;
auto h = m.hash_function();
std::size_t a = h("hello");
std::size_t b = m.hash_function()("hello");
std::cout << std::boolalpha;
std::cout << "same hash both times: " << (a == b) << '\n';
std::cout << "hash is non-zero often: " << (a != 0 || true) << '\n';
return 0;
}
Output
same hash both times: true
hash is non-zero often: true
How It Works
An empty unordered_map still has a default std::hash<std::string>.
Hashing "hello" twice must give the same code, so the first check is true.
We do not print the raw number, because hash values differ across libraries. The lesson is stability for equal keys.
Example 2 — Equal Keys Share a Hash Code
Compare hashes for equal and different keys.
C++
#include <iostream>
#include <string>
#include <unordered_map>
int main() {
std::unordered_map<std::string, int> m = {{"Raj", 25}};
auto h = m.hash_function();
std::string a = "Raj";
std::string b = "Raj";
std::string c = "Mia";
std::cout << std::boolalpha;
std::cout << "h(Raj)==h(Raj): " << (h(a) == h(b)) << '\n';
std::cout << "h(Raj)==h(Mia): " << (h(a) == h(c)) << '\n';
return 0;
}
Output
h(Raj)==h(Raj): true
h(Raj)==h(Mia): false
How It Works
Two strings with the same text must hash the same, so the first line is true.
Different keys usually hash differently. Here "Mia" gets a different code, so the second line is false.
A hash collision (different keys, same code) is still legal; the table handles it with equality checks.
Example 3 — Hash Code Is Not the Bucket Index
Show that bucket() and hash_function() answer different questions.
C++
#include <iostream>
#include <string>
#include <unordered_map>
int main() {
std::unordered_map<std::string, int> m = {{"a", 1}, {"b", 2}};
auto h = m.hash_function();
std::size_t code = h("a");
std::size_t idx = m.bucket("a");
std::cout << std::boolalpha;
std::cout << "bucket index in range: " << (idx < m.bucket_count()) << '\n';
std::cout << "hash code may differ from index: " << (code != idx || code == idx) << '\n';
std::cout << "size: " << m.size() << '\n';
return 0;
}
Output
bucket index in range: true
hash code may differ from index: true
size: 2
How It Works
hash_function()("a") returns a raw hash code.
bucket("a") maps that idea into [0, bucket_count()).
The first check proves the index is valid. We avoid printing the numbers because they are not portable.
Frequently Asked Questions
A copy of the hasher object the unordered_map uses, usually std::hash<Key>.
Call m.hash_function()(key).
No. bucket(key) is an index into the current table. The hash code is a size_t value before that mapping.
Yes. Pass a hash type as a template argument when you create the unordered_map.
They must. That is a requirement of a valid hash function.