Skip to content

Commit 949af9a

Browse files
Enhance key_eq documentation clarity and formatting
Updated formatting and clarified descriptions in the key_eq documentation for unordered_set.
1 parent 177f25f commit 949af9a

File tree

1 file changed

+25
-24
lines changed
  • content/cpp/concepts/unordered-set/terms/key-eq

1 file changed

+25
-24
lines changed

content/cpp/concepts/unordered-set/terms/key-eq/key-eq.md

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,17 @@ The **`key_eq()`** method returns the equality comparison function object used b
2525
unordered_set_name.key_eq();
2626
```
2727

28-
**Return Value:**
28+
**Parameters:**
29+
30+
This method takes no parameters.
31+
32+
**Return value:**
2933

3034
Returns a `key_equal` function object. By default, this is `std::equal_to<T>`, which compares keys using the `==` operator.
3135

3236
## Example
3337

34-
The following example illustrates retrieving and using the equality comparison function from an unordered_set:
38+
The following example illustrates retrieving and using the equality comparison function from an `unordered_set`:
3539

3640
```cpp
3741
#include<iostream>
@@ -41,24 +45,28 @@ using namespace std;
4145

4246
int main() {
4347
unordered_set<int> numbers = {1, 2, 3};
44-
48+
4549
auto eq = numbers.key_eq();
46-
50+
4751
cout << eq(2, 2) << "\n";
4852
cout << eq(2, 3) << "\n";
49-
53+
5054
return 0;
5155
}
5256
```
5357

5458
The above program gives the following output:
5559

56-
```
60+
```shell
5761
1
5862
0
5963
```
6064

61-
> **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.
65+
> **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.
66+
67+
## Codebyte Example
68+
69+
In this example, a custom case-insensitive equality function is used with `unordered_set`, and `key_eq()` retrieves that function to compare two strings while ensuring duplicate keys are not inserted:
6270

6371
```codebyte/cpp
6472
#include<iostream>
@@ -71,47 +79,40 @@ using namespace std;
7179
struct CaseInsensitiveHash {
7280
size_t operator()(const string& str) const {
7381
size_t hash = 0;
74-
82+
7583
for(char ch : str) {
7684
hash = hash * 31 + tolower(ch);
7785
}
78-
86+
7987
return hash;
8088
}
8189
};
8290
8391
struct CaseInsensitiveEq {
8492
bool operator()(const string& a, const string& b) const {
85-
if(a.size() != b.size())
93+
if(a.size() != b.size())
8694
return false;
87-
95+
8896
for(size_t i = 0; i < a.size(); i++) {
89-
if(tolower(a[i]) != tolower(b[i]))
97+
if(tolower(a[i]) != tolower(b[i]))
9098
return false;
9199
}
92-
100+
93101
return true;
94102
}
95103
};
96104
97105
int main() {
98106
unordered_set<string, CaseInsensitiveHash, CaseInsensitiveEq> words;
99-
107+
100108
words.insert("Codecademy");
101109
words.insert("codecademy"); // will be considered equal, and not inserted
102-
110+
103111
auto eq = words.key_eq();
104-
112+
105113
cout << eq("Codecademy", "codecademy") << "\n";
106114
cout << words.size() << "\n";
107-
115+
108116
return 0;
109117
}
110118
```
111-
112-
The above program gives the following output
113-
114-
```
115-
1
116-
1
117-
```

0 commit comments

Comments
 (0)