Skip to content

Add max_size.md term entry for C++ Sets #7209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions content/cpp/concepts/sets/terms/max-size/max-size.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---
Title: 'max_size()'
Description: 'Returns the maximum number of elements the set can hold'
Subjects:
- 'Computer Science'
- 'Data Science'
Tags:
- 'Functions'
- 'Sets'
- 'STL'
CatalogContent:
- 'learn-c-plus-plus'
- 'paths/computer-science'
---

**`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.

## Syntax

```pseudo
set_name.max_size()
```

**Parameters:**

This function does not take any parameters.

**Return value:**

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.

## Example: Using `max_size()` with `std::set`

The following code checks the maximum number of elements a `std::set` can theoretically hold, depending on system and allocator limits:

```cpp
#include <iostream>
#include <set>

int main() {
std::set<int> mySet;
std::cout << "Maximum size of the set: " << mySet.max_size() << std::endl;
return 0;
}
```

The possible output of this code is:

```shell
Maximum size of the set: 4611686018427387903
```

> **Note:** The actual result may vary by system and compiler.

## Codebyte Example: Using `max_size()` with `std::set<std::string>`

Here is a runnable example that demonstrates how to use `max_size()` with a `std::set<std::string>` to check its theoretical capacity:

```codebyte/cpp
#include <iostream>
#include <set>
#include <string>

int main() {
std::set<std::string> stringSet;
std::cout << "Maximum size of the string set: " << stringSet.max_size() << std::endl;
return 0;
}

```