This repository was archived by the owner on Feb 27, 2026. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +14
-11
lines changed
content/cpp/concepts/unordered-set/terms/reserve Expand file tree Collapse file tree 1 file changed +14
-11
lines changed Original file line number Diff line number Diff line change 11---
22Title : ' reserve()'
3- Description : ' Requests capacity change for std::unordered_set so it can accommodate at least n elements without exceeding the maximum load factor.'
3+ Description : ' Requests capacity change for ` std::unordered_set` so it can accommodate at least n elements without exceeding the maximum load factor.'
44Subjects :
55 - ' Code Foundations'
66 - ' Computer Science'
77Tags :
8- - ' Iterators '
8+ - ' Containers '
99 - ' Sets'
1010 - ' STL'
11- - ' Algorithms'
1211CatalogContent :
1312 - ' learn-c-plus-plus'
1413 - ' paths/computer-science'
@@ -28,14 +27,7 @@ unordered_set_name.reserve(n);
2827
2928** Return value:**
3029
31- - ` void `
32-
33- ** Notes:**
34-
35- - Effectively calls ` rehash(ceil(n / max_load_factor())) ` .
36- - May invalidate iterators if a rehash occurs; references and pointers to elements remain valid.
37- - Use ` rehash(k) ` to set the bucket count directly; use ` reserve(n) ` to plan for element count.
38- - Does not impose ordering; iteration order remains implementation-defined.
30+ This method doesn't return anything (` void ` ).
3931
4032## Example
4133
@@ -68,6 +60,17 @@ int main() {
6860}
6961```
7062
63+ The output of this code is:
64+
65+ ``` shell
66+ Buckets before: 3
67+ Buckets after: 11
68+ Max load factor: 1
69+ Load factor: 0.272727
70+ Size: 7
71+ Buckets final: 11
72+ ```
73+
7174## Codebyte Example
7275
7376This example demonstrates ` reserve() ` with strings, and prints capacity-related information:
You can’t perform that action at this time.
0 commit comments