From f6b4c59a3152d9631050e94a544879a87f095054 Mon Sep 17 00:00:00 2001 From: SaiTeja-002 <95877599+SaiTeja-002@users.noreply.github.com> Date: Sat, 20 Dec 2025 16:25:28 +0530 Subject: [PATCH 1/4] Term Entry - C++ Unordered-sets - key_eq() --- .../unordered-set/terms/key-eq/key-eq.md | 115 ++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 content/cpp/concepts/unordered-set/terms/key-eq/key-eq.md 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..6df05a1bd51 --- /dev/null +++ b/content/cpp/concepts/unordered-set/terms/key-eq/key-eq.md @@ -0,0 +1,115 @@ +--- +Title: 'key_eq()' +Description: 'Returns the key equality comparison function used by the unordered set.' +Subjects: + - 'Code Foundations' + - 'Computer Science' +Tags: + - 'Comparison' + - 'Equality' + - 'Functions' + - 'Containers' + - 'Sets' + - 'STL' + - 'Hashes' +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(); +``` + +**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: + +``` +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/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; +} +``` + +The above program gives the following output +``` +1 +1 +``` From 177f25ff27554f2d5419d53633a9c718793cdefd Mon Sep 17 00:00:00 2001 From: SaiTeja-002 <95877599+SaiTeja-002@users.noreply.github.com> Date: Sat, 20 Dec 2025 16:37:47 +0530 Subject: [PATCH 2/4] Fix lint issues --- content/cpp/concepts/unordered-set/terms/key-eq/key-eq.md | 2 ++ 1 file changed, 2 insertions(+) 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 index 6df05a1bd51..f972bfd0843 100644 --- a/content/cpp/concepts/unordered-set/terms/key-eq/key-eq.md +++ b/content/cpp/concepts/unordered-set/terms/key-eq/key-eq.md @@ -57,6 +57,7 @@ The above program gives the following output: 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/cpp @@ -109,6 +110,7 @@ int main() { ``` The above program gives the following output + ``` 1 1 From 949af9a050e11c5ad4c16cc3a7aedb59424e3efb Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Wed, 24 Dec 2025 19:49:57 +0530 Subject: [PATCH 3/4] Enhance key_eq documentation clarity and formatting Updated formatting and clarified descriptions in the key_eq documentation for unordered_set. --- .../unordered-set/terms/key-eq/key-eq.md | 49 ++++++++++--------- 1 file changed, 25 insertions(+), 24 deletions(-) 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 index f972bfd0843..f2a5eb0e4d6 100644 --- a/content/cpp/concepts/unordered-set/terms/key-eq/key-eq.md +++ b/content/cpp/concepts/unordered-set/terms/key-eq/key-eq.md @@ -25,13 +25,17 @@ The **`key_eq()`** method returns the equality comparison function object used b unordered_set_name.key_eq(); ``` -**Return Value:** +**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: +The following example illustrates retrieving and using the equality comparison function from an `unordered_set`: ```cpp #include @@ -41,24 +45,28 @@ 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. +> **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 @@ -71,47 +79,40 @@ 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()) + if(a.size() != b.size()) return false; - + for(size_t i = 0; i < a.size(); i++) { - if(tolower(a[i]) != tolower(b[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; } ``` - -The above program gives the following output - -``` -1 -1 -``` From 259fff7400cda1df92686482697c82f2836423d7 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Wed, 24 Dec 2025 19:51:18 +0530 Subject: [PATCH 4/4] Fix tag duplication in key-eq.md --- content/cpp/concepts/unordered-set/terms/key-eq/key-eq.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 index f2a5eb0e4d6..e5fd1d00e77 100644 --- a/content/cpp/concepts/unordered-set/terms/key-eq/key-eq.md +++ b/content/cpp/concepts/unordered-set/terms/key-eq/key-eq.md @@ -6,12 +6,12 @@ Subjects: - 'Computer Science' Tags: - 'Comparison' + - 'Containers' - 'Equality' - 'Functions' - - 'Containers' + - 'Hashes' - 'Sets' - 'STL' - - 'Hashes' CatalogContent: - 'learn-c-plus-plus' - 'paths/computer-science'