Skip to content

Commit 5a6d242

Browse files
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.
1 parent 1e15d86 commit 5a6d242

File tree

1 file changed

+28
-20
lines changed
  • content/cpp/concepts/unordered-set/terms/max-size

1 file changed

+28
-20
lines changed
Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,63 @@
11
---
2-
Title: 'max_size()' # Required; the file name should be the same as the title, but lowercase, with dashes instead of spaces, and all punctuation removed
3-
Description: 'Returns the maximum number of elements that the container can theoretically hold.' # Required; ideally under 150 characters and starts with a present-tense verb (used in search engine results and content previews)
4-
Subjects: # Please only use Subjects in the subjects.md file (https://github.com/Codecademy/docs/blob/main/documentation/subjects.md). If that list feels insufficient, feel free to create a new Subject and add it to subjects.md in your PR!
5-
- 'Computer Science'
2+
Title: 'max_size()'
3+
Description: 'Returns the maximum number of elements that the container can theoretically hold.'
4+
Subjects:
65
- 'Code Foundations'
7-
Tags: # Please only use Tags in the tags.md file (https://github.com/Codecademy/docs/blob/main/documentation/tags.md). If that list feels insufficient, feel free to create a new Tag and add it to tags.md in your PR!
8-
- 'Methods'
6+
- 'Computer Science'
7+
Tags:
98
- 'Containers'
9+
- 'Methods'
1010
- 'STL'
11-
CatalogContent: # Please use course/path landing page slugs, rather than linking to individual content items. If listing multiple items, please put the most relevant one first
11+
CatalogContent:
1212
- 'learn-c-plus-plus'
1313
- 'paths/computer-science'
1414
---
1515

16-
The **`.max_size()`** method returns the maximum number of elements that a container can theoretically hold. This value depends on system and library implementation limitations, not the actual available memory.
16+
The **`.max_size()`** member function returns the maximum number of elements a container can theoretically hold. This limit depends on the system and the implementation of the standard library, not on actual available memory.
1717

1818
## Syntax
1919

2020
```pseudo
2121
unordered_set_name.max_size()
2222
```
2323

24-
- `unordered_set_name`: The name of the `unordered_set` container.
24+
**Parameters:**
25+
26+
This function takes no parameters.
27+
28+
**Return value:**
2529

2630
The method returns a value of type `size_type`, representing the theoretical maximum number of elements.
2731

2832
## Example 1: Basic Usage
2933

34+
In this example the program prints the maximum number of elements an `unordered_set` can theoretically hold:
35+
3036
```cpp
3137
#include <iostream>
3238
#include <unordered_set>
3339

3440
int main() {
3541
std::unordered_set<int> numbers;
36-
42+
3743
std::cout << "Maximum size: " << numbers.max_size() << std::endl;
38-
44+
3945
return 0;
4046
}
4147
```
4248

4349
This example outputs the maximum possible size of the `unordered_set`:
4450

4551
```shell
46-
Maximum size: 576460752303423487
52+
Maximum size: 1152921504606846975
4753
```
4854

4955
> **Note:** The actual value may vary depending on the system and implementation.
5056
51-
5257
## Example 2: Different Data Types
5358

59+
In this example the `max_size()` value is shown for `unordered_set` containers holding different data types:
60+
5461
```cpp
5562
#include <iostream>
5663
#include <unordered_set>
@@ -59,37 +66,38 @@ int main() {
5966
std::unordered_set<int> int_set;
6067
std::unordered_set<double> double_set;
6168
std::unordered_set<char> char_set;
62-
69+
6370
std::cout << "int max_size: " << int_set.max_size() << std::endl;
6471
std::cout << "double max_size: " << double_set.max_size() << std::endl;
6572
std::cout << "char max_size: " << char_set.max_size() << std::endl;
66-
73+
6774
return 0;
6875
}
6976
```
7077

7178
This demonstrates that `max_size()` can vary based on the element type:
7279

7380
```shell
74-
int max_size: 576460752303423487
75-
double max_size: 384307168202282325
81+
int max_size: 1152921504606846975
82+
double max_size: 1152921504606846975
7683
char max_size: 1152921504606846975
7784
```
7885

7986
## Codebyte Example
8087

88+
In this example the program compares the current size of an `unordered_set` with its theoretical maximum:
8189

8290
```codebyte/cpp
8391
#include <iostream>
8492
#include <unordered_set>
8593
8694
int main() {
8795
std::unordered_set<std::string> fruits = {"apple", "banana", "cherry"};
88-
96+
8997
std::cout << "Current size: " << fruits.size() << std::endl;
9098
std::cout << "Maximum size: " << fruits.max_size() << std::endl;
9199
std::cout << "Available capacity: " << fruits.max_size() - fruits.size() << std::endl;
92-
100+
93101
return 0;
94102
}
95-
```
103+
```

0 commit comments

Comments
 (0)