Skip to content

Commit 68d14c8

Browse files
authored
[Term Entry] C++ Sets: max_size()
* Add max_size.md term entry for C++ Sets with description, syntax, example, and codebyte * correctly changed file-location from sets/max_size/max_size.md -> max_size/max_size.md * content tweak and changed file name * Update max-size.md ---------
1 parent e0dd142 commit 68d14c8

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
Title: 'max_size()'
3+
Description: 'Returns the maximum number of elements the set can hold.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Data Science'
7+
Tags:
8+
- 'Functions'
9+
- 'Sets'
10+
- 'STL'
11+
CatalogContent:
12+
- 'learn-c-plus-plus'
13+
- 'paths/computer-science'
14+
---
15+
16+
**`max_size()`** is a member function of C++ containers like `std::set` that returns the maximum number of elements the container can hold, based on the system or allocator limitations. It does not reflect the current size, but the theoretical upper bound.
17+
18+
## Syntax
19+
20+
```pseudo
21+
set_name.max_size()
22+
```
23+
24+
**Parameters:**
25+
26+
This function does not take any parameters.
27+
28+
**Return value:**
29+
30+
Returns an integral value of type `size_type` (usually `std::size_t`) representing the maximum number of elements the set can theoretically contain, based on system or allocator limitations.
31+
32+
## Example: Using `max_size()` with `std::set`
33+
34+
This code checks the maximum number of elements a `std::set` can theoretically hold, depending on system and allocator limits:
35+
36+
```cpp
37+
#include <iostream>
38+
#include <set>
39+
40+
int main() {
41+
std::set<int> mySet;
42+
std::cout << "Maximum size of the set: " << mySet.max_size() << std::endl;
43+
return 0;
44+
}
45+
```
46+
47+
The possible output of this code is:
48+
49+
```shell
50+
Maximum size of the set: 4611686018427387903
51+
```
52+
53+
> **Note:** The actual result may vary by system and compiler.
54+
55+
## Codebyte Example: Using `max_size()` with `std::set<std::string>`
56+
57+
Here is a runnable codebyte example that demonstrates how to use `max_size()` with a `std::set<std::string>` to check its theoretical capacity:
58+
59+
```codebyte/cpp
60+
#include <iostream>
61+
#include <set>
62+
#include <string>
63+
64+
int main() {
65+
std::set<std::string> stringSet;
66+
std::cout << "Maximum size of the string set: " << stringSet.max_size() << std::endl;
67+
return 0;
68+
}
69+
```

0 commit comments

Comments
 (0)