Skip to content

Commit a685e94

Browse files
[Entry] C++ unordered map: hash_function() (#7390)
* [Entry] C++ unordered map: hash_function() * minor changes * using namepsace in the code snippets to save space * format fix ---------
1 parent cade004 commit a685e94

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
---
2+
Title: 'hash_function()'
3+
Description: 'Returns the hash function used internally by an unordered_map to map keys to buckets.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Data Science'
7+
Tags:
8+
- 'Hash Maps'
9+
- 'STL'
10+
CatalogContent:
11+
- 'learn-cpp'
12+
- 'paths/computer-science'
13+
---
14+
15+
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<Key>`, but it can be customized when the map is declared.
16+
17+
## Syntax
18+
19+
```pseudo
20+
unordered_mapName.hash_function();
21+
```
22+
23+
**Parameters:**
24+
25+
The `hash_function()` method takes no parameters.
26+
27+
**Return value:**
28+
29+
Returns the hash function used by the unordered_map. The return type is the function object used for hashing keys.
30+
31+
## Example 1: Default hash function
32+
33+
Get the hash value of a string key using the default hash function:
34+
35+
```cpp
36+
#include <iostream>
37+
#include <unordered_map>
38+
39+
using namespace std;
40+
41+
int main() {
42+
unordered_map<string, int> myMap;
43+
auto hashFunc = myMap.hash_function();
44+
45+
string key = "gfg";
46+
cout << "Hash value of key '" << key << "' is: " << hashFunc(key) << endl;
47+
return 0;
48+
}
49+
```
50+
51+
The output of this code is:
52+
53+
```shell
54+
Hash value of key 'gfg' is: 2677022477486138405
55+
```
56+
57+
This example shows how the internal hash function can be used to compute a hash value for a given key.
58+
59+
## Example 2: Comparing hash values of keys
60+
61+
Check how different string keys are hashed:
62+
63+
```cpp
64+
#include <iostream>
65+
#include <unordered_map>
66+
67+
using namespace std;
68+
69+
int main() {
70+
unordered_map<string, int> data;
71+
auto hashFn = data.hash_function();
72+
73+
cout << "Hash for 'apple': " << hashFn("apple") << endl;
74+
cout << "Hash for 'banana': " << hashFn("banana") << endl;
75+
return 0;
76+
}
77+
```
78+
79+
The output of the code is:
80+
81+
```shell
82+
Hash for 'apple': 7562681486644061055
83+
Hash for 'banana': 680920345727384150
84+
```
85+
86+
The hash function generates different values for different strings, helping distribute them across buckets.
87+
88+
## Codebyte Example: Hashing integer keys
89+
90+
Visualize how integer keys are hashed in a real-world lookup:
91+
92+
```codebyte/cpp
93+
#include <iostream>
94+
#include <unordered_map>
95+
96+
using namespace std;
97+
98+
int main() {
99+
unordered_map<int, string> lookup;
100+
lookup[101] = "Alice";
101+
lookup[202] = "Bob";
102+
103+
auto h = lookup.hash_function();
104+
cout << "Hash of 101: " << h(101) << "\n";
105+
cout << "Hash of 202: " << h(202) << "\n";
106+
return 0;
107+
}
108+
```
109+
110+
The hash function often returns the value itself for integer keys, as integers map cleanly to buckets.
111+
112+
## Frequently asked questions
113+
114+
### 1. Can a custom hash function be used in unordered_map?
115+
116+
Yes. A user-defined hash function can be passed as a template parameter when defining the map.
117+
118+
### 2. Is hash_function() always std::hash?
119+
120+
By default, yes. But if a custom hash is provided during map declaration, hash_function() returns that.
121+
122+
### 3. When is hash_function() useful?
123+
124+
When debugging or when needing to understand how keys are being distributed across buckets internally.

0 commit comments

Comments
 (0)