Skip to content

Commit f6b4c59

Browse files
authored
Term Entry - C++ Unordered-sets - key_eq()
1 parent 23d0583 commit f6b4c59

File tree

1 file changed

+115
-0
lines changed
  • content/cpp/concepts/unordered-set/terms/key-eq

1 file changed

+115
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
---
2+
Title: 'key_eq()'
3+
Description: 'Returns the key equality comparison function used by the unordered set.'
4+
Subjects:
5+
- 'Code Foundations'
6+
- 'Computer Science'
7+
Tags:
8+
- 'Comparison'
9+
- 'Equality'
10+
- 'Functions'
11+
- 'Containers'
12+
- 'Sets'
13+
- 'STL'
14+
- 'Hashes'
15+
CatalogContent:
16+
- 'learn-c-plus-plus'
17+
- 'paths/computer-science'
18+
---
19+
20+
The **`key_eq()`** method returns the equality comparison function object used by an [`unordered_set`](https://www.codecademy.com/resources/docs/cpp/unordered-set) to determine whether two keys are considered equal.
21+
22+
## Syntax
23+
24+
```pseudo
25+
unordered_set_name.key_eq();
26+
```
27+
28+
**Return Value:**
29+
30+
Returns a `key_equal` function object. By default, this is `std::equal_to<T>`, which compares keys using the `==` operator.
31+
32+
## Example
33+
34+
The following example illustrates retrieving and using the equality comparison function from an unordered_set:
35+
36+
```cpp
37+
#include<iostream>
38+
#include<unordered_set>
39+
40+
using namespace std;
41+
42+
int main() {
43+
unordered_set<int> numbers = {1, 2, 3};
44+
45+
auto eq = numbers.key_eq();
46+
47+
cout << eq(2, 2) << "\n";
48+
cout << eq(2, 3) << "\n";
49+
50+
return 0;
51+
}
52+
```
53+
54+
The above program gives the following output:
55+
56+
```
57+
1
58+
0
59+
```
60+
> **Note:** If two keys are considered equal by key_eq(), they must also produce the same hash value. Failing to maintain this consistency results in undefined behavior.
61+
62+
```codebyte/cpp
63+
#include<iostream>
64+
#include<unordered_set>
65+
#include<string>
66+
#include<cctype>
67+
68+
using namespace std;
69+
70+
struct CaseInsensitiveHash {
71+
size_t operator()(const string& str) const {
72+
size_t hash = 0;
73+
74+
for(char ch : str) {
75+
hash = hash * 31 + tolower(ch);
76+
}
77+
78+
return hash;
79+
}
80+
};
81+
82+
struct CaseInsensitiveEq {
83+
bool operator()(const string& a, const string& b) const {
84+
if(a.size() != b.size())
85+
return false;
86+
87+
for(size_t i = 0; i < a.size(); i++) {
88+
if(tolower(a[i]) != tolower(b[i]))
89+
return false;
90+
}
91+
92+
return true;
93+
}
94+
};
95+
96+
int main() {
97+
unordered_set<string, CaseInsensitiveHash, CaseInsensitiveEq> words;
98+
99+
words.insert("Codecademy");
100+
words.insert("codecademy"); // will be considered equal, and not inserted
101+
102+
auto eq = words.key_eq();
103+
104+
cout << eq("Codecademy", "codecademy") << "\n";
105+
cout << words.size() << "\n";
106+
107+
return 0;
108+
}
109+
```
110+
111+
The above program gives the following output
112+
```
113+
1
114+
1
115+
```

0 commit comments

Comments
 (0)