Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions content/cpp/concepts/unordered-set/terms/key-eq/key-eq.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
---
Title: 'key_eq()'
Description: 'Returns the key equality comparison function used by the unordered set.'
Subjects:
- 'Code Foundations'
- 'Computer Science'
Tags:
- 'Comparison'
- 'Containers'
- 'Equality'
- 'Functions'
- 'Hashes'
- 'Sets'
- 'STL'
CatalogContent:
- 'learn-c-plus-plus'
- 'paths/computer-science'
---

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.

## Syntax

```pseudo
unordered_set_name.key_eq();
```

**Parameters:**

This method takes no parameters.

**Return value:**

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

## Example

The following example illustrates retrieving and using the equality comparison function from an `unordered_set`:

```cpp
#include<iostream>
#include<unordered_set>

using namespace std;

int main() {
unordered_set<int> numbers = {1, 2, 3};

auto eq = numbers.key_eq();

cout << eq(2, 2) << "\n";
cout << eq(2, 3) << "\n";

return 0;
}
```

The above program gives the following output:

```shell
1
0
```

> **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.

## Codebyte Example

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:

```codebyte/cpp
#include<iostream>
#include<unordered_set>
#include<string>
#include<cctype>

using namespace std;

struct CaseInsensitiveHash {
size_t operator()(const string& str) const {
size_t hash = 0;

for(char ch : str) {
hash = hash * 31 + tolower(ch);
}

return hash;
}
};

struct CaseInsensitiveEq {
bool operator()(const string& a, const string& b) const {
if(a.size() != b.size())
return false;

for(size_t i = 0; i < a.size(); i++) {
if(tolower(a[i]) != tolower(b[i]))
return false;
}

return true;
}
};

int main() {
unordered_set<string, CaseInsensitiveHash, CaseInsensitiveEq> words;

words.insert("Codecademy");
words.insert("codecademy"); // will be considered equal, and not inserted

auto eq = words.key_eq();

cout << eq("Codecademy", "codecademy") << "\n";
cout << words.size() << "\n";

return 0;
}
```