diff --git a/content/cpp/concepts/unordered-set/terms/key-eq/key-eq.md b/content/cpp/concepts/unordered-set/terms/key-eq/key-eq.md new file mode 100644 index 00000000000..e5fd1d00e77 --- /dev/null +++ b/content/cpp/concepts/unordered-set/terms/key-eq/key-eq.md @@ -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`, which compares keys using the `==` operator. + +## Example + +The following example illustrates retrieving and using the equality comparison function from an `unordered_set`: + +```cpp +#include +#include + +using namespace std; + +int main() { + unordered_set 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 +#include +#include +#include + +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 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; +} +```