C++ std::unordered_set max_size() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::unordered_set

Definition

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();
CallWhat 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

ParameterDescription
Nonemax_size() takes no parameters. Call it with empty brackets.

Return Value

Return ValueDescription
size_typeThe 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.

C++
#include <iostream>
#include <unordered_set>

int main() {
    std::unordered_set<int> s = {1, 2, 3};
    std::size_t before = s.max_size();

    s.insert(4);
    std::size_t after = s.max_size();

    std::cout << std::boolalpha;
    std::cout << "max_size > 1000: " << (before > 1000) << '\n';
    std::cout << "max_size > size: " << (before > s.size()) << '\n';
    std::cout << "unchanged by insert: " << (before == after) << '\n';
    return 0;
}

How It Works

  1. The program saves max_size() in before, then inserts 4, and saves it again in after.
  2. 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.
  3. 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.

C++
#include <iostream>
#include <unordered_set>

int main() {
    std::unordered_set<int> small = {1};
    std::unordered_set<int> big = {1, 2, 3, 4, 5, 6, 7, 8};

    std::cout << std::boolalpha;
    std::cout << "small.size: " << small.size() << '\n';
    std::cout << "big.size: " << big.size() << '\n';
    std::cout << "same max_size: " << (small.max_size() == big.max_size()) << '\n';
    return 0;
}

How It Works

  1. small holds one value and big holds eight, so their sizes are different.
  2. Both sets have the same type, std::unordered_set<int>, and use the same kind of allocator. The limit is a property of the type, not of the contents.
  3. So the last line is true. An unordered_set of a different value type, such as one with large structs, could report another limit.

Example 3 — max_size() as a Safety Check

Test whether a very large number of new values would still fit under the limit.

C++
#include <iostream>
#include <unordered_set>

bool canAdd(const std::unordered_set<int>& s, std::size_t extra) {
    return extra <= s.max_size() - s.size();
}

int main() {
    std::unordered_set<int> s = {1, 2, 3};

    std::cout << std::boolalpha;
    std::cout << "can add 100: " << canAdd(s, 100) << '\n';
    std::cout << "can add max_size: " << canAdd(s, s.max_size()) << '\n';
    return 0;
}

How It Works

  1. canAdd() works out how much room is left: max_size() - size(). It then checks that the number of new values fits in that room.
  2. Adding 100 values to an unordered_set of 3 is far under the limit, so the first line is true.
  3. 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.

Next: merge()

Keep going through the std::unordered_set functions. Move all new values from another unordered_set into this one (C++17).

merge() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful