From 560a21824ce4b5389a7307667c0667e33862be06 Mon Sep 17 00:00:00 2001 From: Aaren <85843497+AarenMcCall@users.noreply.github.com> Date: Sat, 24 Jan 2026 02:27:14 +0100 Subject: [PATCH 1/3] Add files via upload Term Entry for C++ Unordered sets hash function --- .../terms/hash-function/hash-function.md | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 content/cpp/concepts/unordered-set/terms/hash-function/hash-function.md diff --git a/content/cpp/concepts/unordered-set/terms/hash-function/hash-function.md b/content/cpp/concepts/unordered-set/terms/hash-function/hash-function.md new file mode 100644 index 00000000000..59dfd6af5e5 --- /dev/null +++ b/content/cpp/concepts/unordered-set/terms/hash-function/hash-function.md @@ -0,0 +1,84 @@ +--- +Title: hash?function()' +Description: 'Returns the hash function object used by the unordered set.' +Subjects: + - 'Code Foundations' + - 'Computer Science' +Tags: + - 'Data Structures' + - 'Elements' + - 'Hash Maps' + - 'Sets' +CatalogContent: + - 'learn-c-plus-plus' + - 'paths/computer-science' +--- + +The **`hash_function()`** returns the hash function object used by the [`unordered_set`](https://www.codecademy.com/resources/docs/cpp/unordered-set) container. + + +## Syntax + +```pseudo +unordered_set.hash_function(); +``` + +**Parameters:** + +This method takes no parameters. + +**Return value:** + +The `hash_function()` returns the hash function. + +## Example: Using `hash_function()` to return the hash function + +In this example, `hash_function()` is used to return the hash function of an element in an `unordered_set`: + +```cpp +#include +#include + +int main() { + std::unordered_set s = {10, 20, 30}; + + // get the hash function + auto hf = s.hash_function(); + + std::cout << "Hash of 10: " << hf(10) << std::endl; + std::cout << "Hash of 20: " << hf(20) << std::endl; +} +``` + +The output for this code is: + +```shell +Hash of 10: 10 +Hash of 20: 20 +``` + +> **Note:** The output may vary depending on the specific C++ implementation and hash function. + +## Codebyte Example: Inserting a new element into an unordered set + +In this example, the code demonstrates how to retrieve and use the hash function from an unordered_set to compute hash values for its elements. + +```codebyte/cpp +#include +#include + +int main() { + std::unordered_set values = {10, 20, 30}; + + // Retrieve the hash function used by the unordered_set + auto hashFunc = values.hash_function(); + + // Display hash values of elements + for (int v : values) { + std::cout << "Value: " << v + << ", Hash: " << hashFunc(v) << std::endl; + } + + return 0; +} +``` From 9a41bb547899734de36af42da95a26c08ff19e50 Mon Sep 17 00:00:00 2001 From: Aaren <85843497+AarenMcCall@users.noreply.github.com> Date: Sat, 24 Jan 2026 02:35:15 +0100 Subject: [PATCH 2/3] Update hash-function.md Fixed typo in the Title --- .../unordered-set/terms/hash-function/hash-function.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/content/cpp/concepts/unordered-set/terms/hash-function/hash-function.md b/content/cpp/concepts/unordered-set/terms/hash-function/hash-function.md index 59dfd6af5e5..d216082ef84 100644 --- a/content/cpp/concepts/unordered-set/terms/hash-function/hash-function.md +++ b/content/cpp/concepts/unordered-set/terms/hash-function/hash-function.md @@ -1,5 +1,5 @@ --- -Title: hash?function()' +Title: hash_function()' Description: 'Returns the hash function object used by the unordered set.' Subjects: - 'Code Foundations' @@ -82,3 +82,4 @@ int main() { return 0; } ``` + From e990be8170f8c34db9e30ee45bfffbe489d72f70 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Wed, 28 Jan 2026 15:46:56 +0530 Subject: [PATCH 3/3] Revise hash_function() documentation Updated the documentation for the hash_function() method, including syntax, parameters, return value, and examples. --- .../terms/hash-function/hash-function.md | 41 +++++++++---------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/content/cpp/concepts/unordered-set/terms/hash-function/hash-function.md b/content/cpp/concepts/unordered-set/terms/hash-function/hash-function.md index d216082ef84..78057efd621 100644 --- a/content/cpp/concepts/unordered-set/terms/hash-function/hash-function.md +++ b/content/cpp/concepts/unordered-set/terms/hash-function/hash-function.md @@ -1,5 +1,5 @@ --- -Title: hash_function()' +Title: 'hash_function()' Description: 'Returns the hash function object used by the unordered set.' Subjects: - 'Code Foundations' @@ -7,7 +7,6 @@ Subjects: Tags: - 'Data Structures' - 'Elements' - - 'Hash Maps' - 'Sets' CatalogContent: - 'learn-c-plus-plus' @@ -16,7 +15,6 @@ CatalogContent: The **`hash_function()`** returns the hash function object used by the [`unordered_set`](https://www.codecademy.com/resources/docs/cpp/unordered-set) container. - ## Syntax ```pseudo @@ -29,24 +27,24 @@ This method takes no parameters. **Return value:** -The `hash_function()` returns the hash function. +Returns the hash function object used by the `unordered_set` to hash its elements. ## Example: Using `hash_function()` to return the hash function -In this example, `hash_function()` is used to return the hash function of an element in an `unordered_set`: +In this example, `hash_function()` is used to retrieve the hash function used by an `unordered_set` and apply it to elements: ```cpp #include #include int main() { - std::unordered_set s = {10, 20, 30}; + std::unordered_set s = {10, 20, 30}; - // get the hash function - auto hf = s.hash_function(); + // get the hash function + auto hf = s.hash_function(); - std::cout << "Hash of 10: " << hf(10) << std::endl; - std::cout << "Hash of 20: " << hf(20) << std::endl; + std::cout << "Hash of 10: " << hf(10) << std::endl; + std::cout << "Hash of 20: " << hf(20) << std::endl; } ``` @@ -59,27 +57,26 @@ Hash of 20: 20 > **Note:** The output may vary depending on the specific C++ implementation and hash function. -## Codebyte Example: Inserting a new element into an unordered set +## Codebyte Example: Using the hash function of an unordered set -In this example, the code demonstrates how to retrieve and use the hash function from an unordered_set to compute hash values for its elements. +In this example, the code demonstrates how to retrieve and use the hash function from an `unordered_set` to compute hash values for its elements: ```codebyte/cpp #include #include int main() { - std::unordered_set values = {10, 20, 30}; + std::unordered_set values = {10, 20, 30}; - // Retrieve the hash function used by the unordered_set - auto hashFunc = values.hash_function(); + // Retrieve the hash function used by the unordered_set + auto hashFunc = values.hash_function(); - // Display hash values of elements - for (int v : values) { - std::cout << "Value: " << v - << ", Hash: " << hashFunc(v) << std::endl; - } + // Display hash values of elements + for (int v : values) { + std::cout << "Value: " << v + << ", Hash: " << hashFunc(v) << std::endl; + } - return 0; + return 0; } ``` -