Skip to content

Commit 0044ec7

Browse files
[Term Entry] C++ Unordered Map: rehash()
* [Edit] Python: Python CLI arguments * Update command-line-arguments.md * [Term Entry] PyTorch Tensor Operations: .log2() * [Entry] C++ unordered map: rehash() * Delete docs/content/pytorch/concepts/tensor-operations/terms/log2/log2.md * Update rehash.md * Minor changes ---------
1 parent 84f8b57 commit 0044ec7

File tree

1 file changed

+126
-0
lines changed
  • content/cpp/concepts/unordered-map/terms/rehash

1 file changed

+126
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
---
2+
Title: 'rehash()'
3+
Description: 'Adjusts the number of buckets in an unordered_map to improve hashing performance.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Game Development'
7+
Tags:
8+
- 'Containers'
9+
- 'Map'
10+
- 'Methods'
11+
- 'STL'
12+
CatalogContent:
13+
- 'learn-c-plus-plus'
14+
- 'paths/computer-science'
15+
---
16+
17+
The C++ **`rehash()`** function changes the number of internal buckets in a `std::unordered_map` so the container holds at least the specified number. This operation redistributes all existing elements across the new bucket structure and helps reduce collisions when the container grows.
18+
19+
## Syntax
20+
21+
```pseudo
22+
unordered_map.rehash(n);
23+
```
24+
25+
**Parameters:**
26+
27+
- `n`: Minimum number of buckets the `unordered_map` must allocate.
28+
29+
**Return value:**
30+
31+
This function does not return a value.
32+
33+
## Example 1
34+
35+
In this example, the bucket count of an `unordered_map` is expanded before inserting more elements:
36+
37+
```cpp
38+
#include <iostream>
39+
#include <unordered_map>
40+
41+
int main() {
42+
std::unordered_map<std::string, int> scores = {
43+
{"Alex", 80},
44+
{"Mia", 92},
45+
{"Ray", 88}
46+
};
47+
48+
std::cout << "Initial bucket count: " << scores.bucket_count() << "\n";
49+
50+
scores.rehash(30);
51+
52+
std::cout << "Bucket count after rehash(30): " << scores.bucket_count() << "\n";
53+
54+
return 0;
55+
}
56+
```
57+
58+
The output of this code is:
59+
60+
```shell
61+
Initial bucket count: 3
62+
Bucket count after rehash(30): 31
63+
```
64+
65+
## Example 2
66+
67+
In this example, the `unordered_map` is rehashed to show how the internal structure adjusts even when the container is small:
68+
69+
```cpp
70+
#include <iostream>
71+
#include <unordered_map>
72+
73+
int main() {
74+
std::unordered_map<int, int> mapping = {{1, 10}, {2, 20}, {3, 30}};
75+
76+
std::cout << "Before rehash: " << mapping.bucket_count() << "\n";
77+
78+
mapping.rehash(50);
79+
80+
std::cout << "After rehash: " << mapping.bucket_count() << "\n";
81+
82+
return 0;
83+
}
84+
```
85+
86+
The output of this code is:
87+
88+
```shell
89+
Before rehash: 3
90+
After rehash: 53
91+
```
92+
93+
## Codebyte Example
94+
95+
In this example, the bucket count changes after calling rehash on a small `unordered_map`:
96+
97+
```codebyte/cpp
98+
#include <iostream>
99+
#include <unordered_map>
100+
101+
int main() {
102+
std::unordered_map<char, int> freq = {{'a', 3}, {'b', 5}, {'c', 2}};
103+
104+
std::cout << "Bucket count before: " << freq.bucket_count() << "\n";
105+
106+
freq.rehash(20);
107+
108+
std::cout << "Bucket count after: " << freq.bucket_count() << "\n";
109+
110+
return 0;
111+
}
112+
```
113+
114+
## Frequently Asked Questions
115+
116+
### 1. What is the rehash() function in C++?
117+
118+
The `rehash()` function forces an unordered container to reorganize its internal bucket count so it has at least the number provided. This helps control the load factor and reduces collisions in hash-based lookups. It is typically used when anticipating container growth.
119+
120+
### 2. What is the difference between unordered_set and unordered_map in C++?
121+
122+
`unordered_set` stores only unique keys, while `unordered_map` stores key–value pairs. Both provide average constant-time lookups based on hashing, but `unordered_map` supports retrieval of associated values, whereas `unordered_set` focuses solely on whether a key exists.
123+
124+
### 3. What is the use of unordered_set in C++?
125+
126+
`unordered_set` offers fast insertion, deletion, and search of unique keys using hash tables. It is useful when values do not need to be associated with keys and when order does not matter, but fast existence checks do.

0 commit comments

Comments
 (0)