In C++, std::unordered_map::key_eq() returns a copy of the key equality object.
After hashing points at a bucket, the unordered_map uses this predicate to decide if two keys are the same. The default calls operator== on the keys. 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 key_eq, then call the returned object with two keys.
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_map 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.