C++ std::unordered_set key_eq() Function

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

Definition

In C++, std::unordered_set::key_eq() returns a copy of the value equality object.

After hashing points at a bucket, the unordered_set uses this predicate to decide if two values are the same. The default calls operator== on the keys. 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 key_eq, then call the returned object with two values.

C++
auto eq = m.key_eq();
bool same = eq("Raj", "Raj");     // true
bool diff = m.key_eq()("Raj", "Mia");
CallWhat it does
m.key_eq()Returns the key_equal object.
m.key_eq()(a, b)Returns true if a and b are considered the same key.

Unlike std::map, an unordered_set does not order keys with <. It hashes and then tests equality. If you change how equality works, you must also provide a matching hash.

Library declarations
C++
key_equal key_eq() const;

Parameters

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

Return Value

Return ValueDescription
key_equalA copy of the equality predicate. Often std::equal_to<Key>.

C++ Examples

Example 1 — key_eq() Uses Equality

Compare equal and different strings with the predicate.

C++
#include <iostream>
#include <string>
#include <unordered_set>

int main() {
    std::unordered_set<std::string> m;
    auto eq = m.key_eq();

    std::cout << std::boolalpha;
    std::cout << "eq(Raj, Raj): " << eq("Raj", "Raj") << '\n';
    std::cout << "eq(Raj, Mia): " << eq("Raj", "Mia") << '\n';
    return 0;
}

How It Works

  1. The default predicate is based on operator== for std::string.
  2. Identical text compares equal, so the first line is true.
  3. Different text compares unequal, so the second line is false.

Example 2 — find() Needs Hash and Equality

Show that find succeeds only for an equal key.

C++
#include <iostream>
#include <string>
#include <unordered_set>

int main() {
    std::unordered_set<std::string> age = {"Raj"};
    auto eq = age.key_eq();

    std::string query = "Raj";
    auto it = age.find(query);

    std::cout << std::boolalpha;
    std::cout << "keys equal: " << eq(query, "Raj") << '\n';
    std::cout << "find ok: " << (it != age.end()) << '\n';
    std::cout << "found: " << *it << '\n';
    return 0;
}

How It Works

  1. key_eq confirms the query string equals the stored key.
  2. find uses hash + equality under the hood and returns a valid iterator.
  3. The value 25 is read through that iterator.

Example 3 — Equality Versus Ordering

Remind that unordered_set checks equality, not less-than order.

C++
#include <iostream>
#include <string>
#include <unordered_set>

int main() {
    std::unordered_set<int> m = {10, 20};
    auto eq = m.key_eq();

    std::cout << std::boolalpha;
    std::cout << "eq(10, 10): " << eq(10, 10) << '\n';
    std::cout << "eq(10, 20): " << eq(10, 20) << '\n';
    std::cout << "has 20: " << (m.find(20) != m.end()) << '\n';
    return 0;
}

How It Works

  1. key_eq(10, 10) is true because the integers are equal.
  2. key_eq(10, 20) is false. There is no "who is smaller" test here, unlike std::map::key_comp.
  3. find(20) still works because hashing + equality locate the value.

Frequently Asked Questions

A copy of the value equality predicate used by the unordered_set.
Usually std::equal_to<Key>, which uses operator==.
map orders keys with a less-than style compare. unordered_set checks equality after hashing and does not keep keys sorted.
Yes, as a template argument. You must also provide a hash that agrees with that equality.
No. It only compares two values.

Next: load_factor()

Keep going through the std::unordered_set functions. Average number of elements per bucket right now.

load_factor() 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.

10 people found this page helpful