In C++, std::unordered_set::max_size() returns the largest number of values the unordered_set could hold in theory. It is a limit unordered_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 <unordered_set> to use it.
This is a member of std::unordered_set<T>, a hash table of unique values. Find, insert, and erase are usually average constant time. Unlike std::set, values are not sorted and iteration order is unspecified. Elements are const, so you cannot change a stored value through an iterator. There is no operator[] and no at(). Prefer find() or contains() to look up a value, and call reserve() before many inserts.
Syntax
Call it with empty brackets. It takes no value.
C++
std::unordered_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 some 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 an unordered_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 an unordered_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. An unordered_set will run out of memory long before it reaches max_size().
No. It stays the same for a given unordered_set type. Only size() changes when you insert or erase.
It is based on some 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.