From 2101390b42d3d97dbde442f58c7f003f30b5842a Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Sat, 2 Aug 2025 19:39:11 +0530 Subject: [PATCH 1/4] [Entry] C++ unordered map: hash_function() --- .../terms/hash-function/hash-function.md | 118 ++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 content/cpp/concepts/unordered-map/terms/hash-function/hash-function.md diff --git a/content/cpp/concepts/unordered-map/terms/hash-function/hash-function.md b/content/cpp/concepts/unordered-map/terms/hash-function/hash-function.md new file mode 100644 index 00000000000..6762a92b96f --- /dev/null +++ b/content/cpp/concepts/unordered-map/terms/hash-function/hash-function.md @@ -0,0 +1,118 @@ +--- +Title: 'hash_function()' +Description: 'Returns the hash function used internally by an unordered_map to map keys to buckets.' +Subjects: + - 'Computer Science' + - 'Data Science' +Tags: + - 'STL' + - 'Hash Maps' +CatalogContent: + - 'learn-cpp' + - 'paths/computer-science' +--- + +The **`unordered_map::hash_function()`** method returns a function object that the container uses to hash keys. This function determines which bucket an element will be placed in. By default, it uses `std::hash`, but it can be customized when the map is declared. + +## Syntax + +```pseudo +unordered_mapName.hash_function(); +``` + +**Parameters:** + +The `hash_function()` method takes no parameters. + +**Return value:** + +Returns the hash function used by the unordered_map. The return type is the function object used for hashing keys. + +## Example 1: Default hash function + +Get the hash value of a string key using the default hash function: + +```cpp +#include +#include + +int main() { + std::unordered_map myMap; + auto hashFunc = myMap.hash_function(); + + std::string key = "gfg"; + std::cout << "Hash value of key '" << key << "' is: " << hashFunc(key) << std::endl; + return 0; +} +``` + +The output of this code is: + +```shell +Hash value of key 'gfg' is: 2677022477486138405 +``` + +This example shows how the internal hash function can be used to compute a hash value for a given key. + +## Example 2: Comparing hash values of keys + +Check how different string keys are hashed: + +```cpp +#include +#include + +int main() { + std::unordered_map data; + auto hashFn = data.hash_function(); + + std::cout << "Hash for 'apple': " << hashFn("apple") << std::endl; + std::cout << "Hash for 'banana': " << hashFn("banana") << std::endl; + return 0; +} +``` + +The output of the code is: + +```shell +Hash for 'apple': 7562681486644061055 +Hash for 'banana': 680920345727384150 +``` + +The hash function generates different values for different strings, helping distribute them across buckets. + +## Codebyte Example: Hashing integer keys + +Visualize how integer keys are hashed in a real-world lookup: + +```codebyte/cpp +#include +#include + +int main() { + std::unordered_map lookup; + lookup[101] = "Alice"; + lookup[202] = "Bob"; + + auto h = lookup.hash_function(); + std::cout << "Hash of 101: " << h(101) << "\n"; + std::cout << "Hash of 202: " << h(202) << "\n"; + return 0; +} +``` + +For integer keys, the hash function often returns the value itself, as integers map cleanly to buckets. + +## Frequently asked questions + +### 1. Can a custom hash function be used in unordered_map? + +Yes. A user-defined hash function can be passed as a template parameter when defining the map. + +### 2. Is hash_function() always std::hash? + +By default, yes. But if a custom hash is provided during map declaration, hash_function() returns that. + +### 3. When is hash_function() useful? + +When debugging or when needing to understand how keys are being distributed across buckets internally. From ac4b229fea6196545cc32fd657d47cc0ad6744fd Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 6 Aug 2025 13:58:58 +0530 Subject: [PATCH 2/4] minor changes --- .../unordered-map/terms/hash-function/hash-function.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/content/cpp/concepts/unordered-map/terms/hash-function/hash-function.md b/content/cpp/concepts/unordered-map/terms/hash-function/hash-function.md index 6762a92b96f..3630560b37b 100644 --- a/content/cpp/concepts/unordered-map/terms/hash-function/hash-function.md +++ b/content/cpp/concepts/unordered-map/terms/hash-function/hash-function.md @@ -5,14 +5,14 @@ Subjects: - 'Computer Science' - 'Data Science' Tags: - - 'STL' - 'Hash Maps' + - 'STL' CatalogContent: - 'learn-cpp' - 'paths/computer-science' --- -The **`unordered_map::hash_function()`** method returns a function object that the container uses to hash keys. This function determines which bucket an element will be placed in. By default, it uses `std::hash`, but it can be customized when the map is declared. +The **`unordered_map::hash_function()`** method returns a function object the container uses to hash keys. This function determines which bucket an element will be placed in. By default, it uses `std::hash`, but it can be customized when the map is declared. ## Syntax @@ -101,7 +101,7 @@ int main() { } ``` -For integer keys, the hash function often returns the value itself, as integers map cleanly to buckets. +The hash function often returns the value itself for integer keys, as integers map cleanly to buckets. ## Frequently asked questions From a6478d3b20b8f7e506df5162fb23c04c062c2ce5 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 6 Aug 2025 14:01:35 +0530 Subject: [PATCH 3/4] using namepsace in the code snippets to save space --- .../terms/hash-function/hash-function.md | 36 +++++++++++-------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/content/cpp/concepts/unordered-map/terms/hash-function/hash-function.md b/content/cpp/concepts/unordered-map/terms/hash-function/hash-function.md index 3630560b37b..42e29ebfe85 100644 --- a/content/cpp/concepts/unordered-map/terms/hash-function/hash-function.md +++ b/content/cpp/concepts/unordered-map/terms/hash-function/hash-function.md @@ -26,7 +26,7 @@ The `hash_function()` method takes no parameters. **Return value:** -Returns the hash function used by the unordered_map. The return type is the function object used for hashing keys. +Returns the hash function used by the unordered\_map. The return type is the function object used for hashing keys. ## Example 1: Default hash function @@ -36,12 +36,14 @@ Get the hash value of a string key using the default hash function: #include #include +using namespace std; + int main() { - std::unordered_map myMap; + unordered_map myMap; auto hashFunc = myMap.hash_function(); - std::string key = "gfg"; - std::cout << "Hash value of key '" << key << "' is: " << hashFunc(key) << std::endl; + string key = "gfg"; + cout << "Hash value of key '" << key << "' is: " << hashFunc(key) << endl; return 0; } ``` @@ -62,12 +64,14 @@ Check how different string keys are hashed: #include #include +using namespace std; + int main() { - std::unordered_map data; + unordered_map data; auto hashFn = data.hash_function(); - std::cout << "Hash for 'apple': " << hashFn("apple") << std::endl; - std::cout << "Hash for 'banana': " << hashFn("banana") << std::endl; + cout << "Hash for 'apple': " << hashFn("apple") << endl; + cout << "Hash for 'banana': " << hashFn("banana") << endl; return 0; } ``` @@ -75,7 +79,7 @@ int main() { The output of the code is: ```shell -Hash for 'apple': 7562681486644061055 +Hash for 'apple': 7562681486644061055 Hash for 'banana': 680920345727384150 ``` @@ -89,14 +93,16 @@ Visualize how integer keys are hashed in a real-world lookup: #include #include +using namespace std; + int main() { - std::unordered_map lookup; + unordered_map lookup; lookup[101] = "Alice"; lookup[202] = "Bob"; auto h = lookup.hash_function(); - std::cout << "Hash of 101: " << h(101) << "\n"; - std::cout << "Hash of 202: " << h(202) << "\n"; + cout << "Hash of 101: " << h(101) << "\n"; + cout << "Hash of 202: " << h(202) << "\n"; return 0; } ``` @@ -105,14 +111,14 @@ The hash function often returns the value itself for integer keys, as integers m ## Frequently asked questions -### 1. Can a custom hash function be used in unordered_map? +### 1. Can a custom hash function be used in unordered\_map? Yes. A user-defined hash function can be passed as a template parameter when defining the map. -### 2. Is hash_function() always std::hash? +### 2. Is hash\_function() always std::hash? -By default, yes. But if a custom hash is provided during map declaration, hash_function() returns that. +By default, yes. But if a custom hash is provided during map declaration, hash\_function() returns that. -### 3. When is hash_function() useful? +### 3. When is hash\_function() useful? When debugging or when needing to understand how keys are being distributed across buckets internally. From 916142b777f8aea7bcbc01008ba483ca6c6f4a1f Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 6 Aug 2025 14:09:12 +0530 Subject: [PATCH 4/4] format fix --- .../terms/hash-function/hash-function.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/content/cpp/concepts/unordered-map/terms/hash-function/hash-function.md b/content/cpp/concepts/unordered-map/terms/hash-function/hash-function.md index 42e29ebfe85..b38ca37621b 100644 --- a/content/cpp/concepts/unordered-map/terms/hash-function/hash-function.md +++ b/content/cpp/concepts/unordered-map/terms/hash-function/hash-function.md @@ -26,7 +26,7 @@ The `hash_function()` method takes no parameters. **Return value:** -Returns the hash function used by the unordered\_map. The return type is the function object used for hashing keys. +Returns the hash function used by the unordered_map. The return type is the function object used for hashing keys. ## Example 1: Default hash function @@ -79,7 +79,7 @@ int main() { The output of the code is: ```shell -Hash for 'apple': 7562681486644061055 +Hash for 'apple': 7562681486644061055 Hash for 'banana': 680920345727384150 ``` @@ -111,14 +111,14 @@ The hash function often returns the value itself for integer keys, as integers m ## Frequently asked questions -### 1. Can a custom hash function be used in unordered\_map? +### 1. Can a custom hash function be used in unordered_map? Yes. A user-defined hash function can be passed as a template parameter when defining the map. -### 2. Is hash\_function() always std::hash? +### 2. Is hash_function() always std::hash? -By default, yes. But if a custom hash is provided during map declaration, hash\_function() returns that. +By default, yes. But if a custom hash is provided during map declaration, hash_function() returns that. -### 3. When is hash\_function() useful? +### 3. When is hash_function() useful? When debugging or when needing to understand how keys are being distributed across buckets internally.