Skip to content

Commit 66f1808

Browse files
[Term Entry] C++ Unordered Map: .key_eq()
* [Entry] C++ unordered map: key_eq() #7146 Added Entry for key_eq() with syntax and examples as required. * content tweaks * Update key-eq.md * Update key-eq.md ---------
1 parent d6886ab commit 66f1808

File tree

1 file changed

+117
-0
lines changed
  • content/cpp/concepts/unordered-map/terms/key-eq

1 file changed

+117
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
---
2+
Title: '.key_eq()'
3+
Description: 'Returns the key equality comparison function used by the `unordered_map` container.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Data Scienec'
7+
Tags:
8+
- 'Classes'
9+
- 'Map'
10+
- 'OOP'
11+
CatalogContent:
12+
- 'learn-example-course'
13+
- 'paths/example-path'
14+
---
15+
16+
**`.key_eq()`** is a member [function](https://www.codecademy.com/resources/docs/cpp/functions) 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.
17+
18+
## Syntax
19+
20+
```pseudo
21+
unordered_map_name.key_eq()(args1, args2)
22+
```
23+
24+
**Parameters:**
25+
26+
- `key1`: The first key to compare.
27+
- `key2`: The second key to compare.
28+
29+
**Return value:**
30+
31+
- Returns `true` if `key1` and `key2` are considered equal by the map's key comparison function.
32+
- Returns `false` otherwise.
33+
34+
## Example
35+
36+
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:
37+
38+
```cpp
39+
#include <iostream>
40+
#include <unordered_map>
41+
#include <vector>
42+
#include <string>
43+
44+
int main() {
45+
std::unordered_map<std::string, int> animalPopulations = {
46+
{"Tiger", 5574},
47+
{"Elephant", 415000},
48+
{"Giraffe", 117000},
49+
{"Rhinoceros", 27000}
50+
};
51+
52+
// List of animals to look up
53+
std::vector<std::string> animalList = {"Tiger", "Giraffe"};
54+
55+
// Get the equality function used by the map
56+
auto eq = animalPopulations.key_eq();
57+
58+
// Loop through the list
59+
for (const std::string& target : animalList) {
60+
for (const auto& pair : animalPopulations) {
61+
if (eq(pair.first, target)) {
62+
std::cout << "There are " << pair.second << " " << pair.first << "s in the wild." << std::endl;
63+
}
64+
}
65+
}
66+
67+
return 0;
68+
}
69+
```
70+
71+
The output of this code is:
72+
73+
```shell
74+
There are 5574 Tigers in the wild.
75+
There are 117000 Giraffes in the wild.
76+
```
77+
78+
## Codebyte Example
79+
80+
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:
81+
82+
```codebyte/cpp
83+
#include <iostream>
84+
#include <unordered_map>
85+
#include <vector>
86+
#include <string>
87+
88+
int main() {
89+
std::unordered_map<std::string, std::string> proteinSource = {
90+
{"Chicken", "non-veg"},
91+
{"Salmon", "non-veg"},
92+
{"Greek Yogurt", "veg"},
93+
{"Cottage Cheese", "veg"}
94+
};
95+
96+
// List of foods to look up
97+
std::vector<std::string> foodList = {"Chicken", "Greek Yogurt", "Paneer"};
98+
99+
auto eq = proteinSource.key_eq();
100+
101+
for (const std::string& food : foodList) {
102+
bool found = false;
103+
for (const auto& pair : proteinSource) {
104+
if (eq(pair.first, food)) {
105+
std::cout << pair.first << " is a " << pair.second << " source of protein" << std::endl;
106+
found = true;
107+
break;
108+
}
109+
}
110+
if (!found) {
111+
std::cout << food << " is not found in the protein source list." << std::endl;
112+
}
113+
}
114+
115+
return 0;
116+
}
117+
```

0 commit comments

Comments
 (0)