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");
Call
What 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
Parameter
Description
None
key_eq() takes no parameters. Call it with empty brackets.
Return Value
Return Value
Description
key_equal
A 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.