In C++, std::set::max_size() returns the largest number of values the set could hold in theory. It is a limit set by the library and the memory system, not a promise that you can reach it.
The number is huge, usually in the billions or more, and it differs between compilers and systems. Include <set> to use it.
This is a member of std::set<T>, a sorted container of unique values. A set never keeps a duplicate and always holds its values in sorted order, so it can find, insert, and erase a value in logarithmic time. A set has no operator[] and no at(), because the value is its own key. Every element is const, so you cannot change a value through an iterator.
Syntax
Call it with empty brackets. It takes no value.
C++
std::set<int> s;
std::size_t limit = s.max_size(); // a very large number
bool roomy = s.max_size() > s.size();
Call
What it does
s.max_size()
Returns the largest possible number of values as a size_type.
Never print max_size() and expect the same number on every computer. It depends on the value type and the platform. A real set runs out of memory long before it reaches this limit.
Library declarations
C++
size_type max_size() const noexcept;
Parameters
Parameter
Description
None
max_size() takes no parameters. Call it with empty brackets.
Return Value
Return Value
Description
size_type
The theoretical maximum number of values. It is always at least size().
C++ Examples
Example 1 — max_size() Is Bigger Than size()
Check that the limit is large and that it stays the same when you insert.
The program saves max_size() in before, then inserts 4, and saves it again in after.
Instead of printing the exact number, which changes between systems, the program prints comparisons. The limit is far bigger than 1000 and bigger than the current size of 4.
before == after is true. Inserting a value changes size() but never changes max_size().
Example 2 — max_size() Depends on the Type
Two sets of the same type report the same limit, no matter how many values they hold.
canAdd() works out how much room is left: max_size() - size(). It then checks that the number of new values fits in that room.
Adding 100 values to a set of 3 is far under the limit, so the first line is true.
Adding max_size() more values cannot fit, because 3 are already stored. The room is smaller than the request, so the second line is false. Subtracting first avoids an overflow that size() + extra could cause.
Frequently Asked Questions
The largest number of values a set could hold in theory. The exact number depends on your compiler, the platform, and the value type.
No. It is a limit from the library, not a measure of free memory. A set will run out of memory long before it reaches max_size().
No. It stays the same for a given set type. Only size() changes when you insert or erase.
It is based on the biggest number the address space and the node size allow. The value is a ceiling and not a target to reach.
Rarely. It is useful for a safety check before a very large insert, or when you write generic code that works with any container.