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/2] 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/2] 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; } ``` +