Skip to content
This repository was archived by the owner on Feb 27, 2026. It is now read-only.

Commit d2da492

Browse files
authored
[Term Entry] C++ Unordered-sets: max_size()
* Add max_size entry * Change folder name * Revise max_size() documentation and examples Updated the max_size documentation to clarify its usage and examples. Adjusted return values and improved syntax for better readability. * Remove duplicate max_size folder, keeping max-size * Update max-size.md ---------
1 parent 12028a0 commit d2da492

File tree

1 file changed

+103
-0
lines changed
  • content/cpp/concepts/unordered-set/terms/max-size

1 file changed

+103
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
---
2+
Title: 'max_size()'
3+
Description: 'Returns the maximum number of elements that an unordered_set can theoretically hold.'
4+
Subjects:
5+
- 'Code Foundations'
6+
- 'Computer Science'
7+
Tags:
8+
- 'Containers'
9+
- 'Methods'
10+
- 'STL'
11+
CatalogContent:
12+
- 'learn-c-plus-plus'
13+
- 'paths/computer-science'
14+
---
15+
16+
The **`.max_size()`** member [function](https://www.codecademy.com/resources/docs/cpp/functions) returns the maximum number of elements an `unordered_set` can theoretically hold. This limit depends on the system and the implementation of the standard library, not on actual available memory.
17+
18+
## Syntax
19+
20+
```pseudo
21+
unordered_set_name.max_size()
22+
```
23+
24+
**Parameters:**
25+
26+
This function takes no parameters.
27+
28+
**Return value:**
29+
30+
The method returns a value of type `size_type`, representing the theoretical maximum number of elements.
31+
32+
## Example 1: Basic Usage
33+
34+
In this example, the program prints the maximum number of elements an `unordered_set` can theoretically hold:
35+
36+
```cpp
37+
#include <iostream>
38+
#include <unordered_set>
39+
40+
int main() {
41+
std::unordered_set<int> numbers;
42+
43+
std::cout << "Maximum size: " << numbers.max_size() << std::endl;
44+
45+
return 0;
46+
}
47+
```
48+
49+
The output will be:
50+
51+
```shell
52+
Maximum size: 1152921504606846975
53+
```
54+
55+
> **Note:** The actual value may vary depending on the system and implementation.
56+
57+
## Example 2: Using Different Data Types
58+
59+
In this example, the `max_size()` value is shown for `unordered_set` containers holding different [data types](https://www.codecademy.com/resources/docs/cpp/data-types):
60+
61+
```cpp
62+
#include <iostream>
63+
#include <unordered_set>
64+
65+
int main() {
66+
std::unordered_set<int> int_set;
67+
std::unordered_set<double> double_set;
68+
std::unordered_set<char> char_set;
69+
70+
std::cout << "int max size: " << int_set.max_size() << std::endl;
71+
std::cout << "double max size: " << double_set.max_size() << std::endl;
72+
std::cout << "char max size: " << char_set.max_size() << std::endl;
73+
74+
return 0;
75+
}
76+
```
77+
78+
The output will be:
79+
80+
```shell
81+
int max size: 1152921504606846975
82+
double max size: 1152921504606846975
83+
char max size: 1152921504606846975
84+
```
85+
86+
## Codebyte Example
87+
88+
In this example, the program compares the current size of an `unordered_set` with its theoretical maximum:
89+
90+
```codebyte/cpp
91+
#include <iostream>
92+
#include <unordered_set>
93+
94+
int main() {
95+
std::unordered_set<std::string> fruits = {"apple", "banana", "cherry"};
96+
97+
std::cout << "Current size: " << fruits.size() << std::endl;
98+
std::cout << "Maximum size: " << fruits.max_size() << std::endl;
99+
std::cout << "Available capacity: " << fruits.max_size() - fruits.size() << std::endl;
100+
101+
return 0;
102+
}
103+
```

0 commit comments

Comments
 (0)