C++ .key_eq()

Anonymous contributor's avatar
Anonymous contributor
Published Aug 1, 2025
Contribute to Docs

.key_eq() is a member function that returns the function (also called a predicate) that the container uses to determine whether two keys are considered equal. This function is essential because, in an unordered_map, keys are stored in hash buckets and even if two keys hash to the same bucket, the container still needs to check if they’re actually equal. That’s where .key_eq() comes in.

Syntax

unordered_map_name.key_eq()(args1, args2)

Parameters:

  • key1: The first key to compare.
  • key2: The second key to compare.

Return value:

  • Returns true if key1 and key2 are considered equal by the map’s key comparison function.
  • Returns false otherwise.

Example

In this example, the map’s key equality function is used to find and print the population of selected animals by comparing external values to map keys:

#include <iostream>
#include <unordered_map>
#include <vector>
#include <string>
int main() {
std::unordered_map<std::string, int> animalPopulations = {
{"Tiger", 5574},
{"Elephant", 415000},
{"Giraffe", 117000},
{"Rhinoceros", 27000}
};
// List of animals to look up
std::vector<std::string> animalList = {"Tiger", "Giraffe"};
// Get the equality function used by the map
auto eq = animalPopulations.key_eq();
// Loop through the list
for (const std::string& target : animalList) {
for (const auto& pair : animalPopulations) {
if (eq(pair.first, target)) {
std::cout << "There are " << pair.second << " " << pair.first << "s in the wild." << std::endl;
}
}
}
return 0;
}

The output of this code is:

There are 5574 Tigers in the wild.
There are 117000 Giraffes in the wild.

Codebyte Example

In this example, the key equality function is used to check whether foods from a list exist in the map and print their protein type or a not-found message:

Code
Output
Loading...

All contributors

Contribute to Docs