C++ std::unordered_set hash_function() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::unordered_set

Definition

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");
CallWhat 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

ParameterDescription
Nonehash_function() takes no parameters. Call it with empty brackets.

Return Value

Return ValueDescription
hasherA 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;
}

How It Works

  1. An empty unordered_set still has a default std::hash<std::string>.
  2. Hashing "hello" twice must give the same code, so the first check is true.
  3. 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;
}

How It Works

  1. Two strings with the same text must hash the same, so the first line is true.
  2. Different values usually hash differently. Here "Mia" gets a different code, so the second line is false.
  3. 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;
}

How It Works

  1. hash_function()("a") returns a raw hash code.
  2. bucket("a") maps that idea into [0, bucket_count()).
  3. 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.

Next: insert()

Keep going through the std::unordered_set functions. Add a value to the unordered_set. A repeated value is ignored.

insert() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful