In C++, std::unordered_set::hash_function() returns a copy of the hash function object the container uses.
A hash function turns a value into a number. The unordered_set then picks a bucket from that number. The default is std::hash<Key>. Include <unordered_set> to use it.
This is a member of std::unordered_set<T>, a hash table of unique values. Find, insert, and erase are usually average constant time. Unlike std::set, values are not sorted and iteration order is unspecified. Elements are const, so you cannot change a stored value through an iterator. There is no operator[] and no at(). Prefer find() or contains() to look up a value, and call reserve() before many inserts.
Syntax
Call hash_function, then call the returned object on a value.
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_set.
m.hash_function()(key)
Hashes key and returns a std::size_t code.
Equal values must produce equal hash codes. Different values 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_set>
int main() {
std::unordered_set<std::string> 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_set 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_set>
int main() {
std::unordered_set<std::string> m = {"Raj"};
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 values 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_set>
int main() {
std::unordered_set<std::string> m = {"a", "b"};
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_set 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_set.
They must. That is a requirement of a valid hash function.