C++ std::unordered_map max_size() Function

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

Definition

In C++, std::unordered_map::max_size() returns the largest number of pairs an unordered_map of this type could possibly hold. It is a limit set by the library and the size of one node.

The number is usually enormous, so you will rarely reach it. A real program runs out of memory long before. It is mostly useful for generic code and for learning how containers work. Include <unordered_map> to use it.

This is a member of std::unordered_map<Key, T>, a hash table of key and value pairs. Each key is unique. Find, insert, and erase are usually average constant time. Unlike std::map, keys are not sorted and iteration order is unspecified. Read a value with at() or find(). The m[key] operator also works, but it adds a default value when the key is missing.

Syntax

Call max_size with empty brackets. It returns an unsigned number.

C++
std::size_t limit = m.max_size();   // theoretical maximum
bool roomLeft = m.size() < m.max_size();
CallWhat it does
m.max_size()Returns the maximum number of elements the unordered_map could hold, as size_type.

max_size() and size() answer different questions. size() is how many pairs you have now. max_size() is how many the type allows in theory. The exact value is implementation-defined, so never print it in a test and expect the same number everywhere.

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 upper limit on the number of pairs. It does not tell you how much memory is free.

C++ Examples

Example 1 — max_size() Is Larger Than size()

Show that the limit is always at least as big as the number of pairs in the unordered_map.

C++
#include <iostream>
#include <unordered_map>
#include <string>

int main() {
    std::unordered_map<std::string, int> m = {{"a", 1}, {"b", 2}, {"c", 3}};
    std::cout << std::boolalpha;

    std::cout << "size: " << m.size() << '\n';
    std::cout << "max_size > size: " << (m.max_size() > m.size()) << '\n';
    return 0;
}

How It Works

  1. The unordered_map holds three pairs, so size() is 3.
  2. max_size() returns a very large number, far above three. It is not printed on purpose, because it differs between compilers and systems.
  3. The comparison m.max_size() > m.size() is true, and boolalpha prints it as the word true.

Example 2 — max_size() Is Very Large

Check that the limit is far beyond what a normal program stores.

C++
#include <iostream>
#include <unordered_map>

int main() {
    std::unordered_map<int, int> m;
    std::cout << std::boolalpha;

    bool huge = m.max_size() > 1000000;
    std::cout << "holds over a million pairs in theory: " << huge << '\n';
    std::cout << "pairs stored: " << m.size() << '\n';
    return 0;
}

How It Works

  1. An empty std::unordered_map<int, int> is created. Nothing is stored yet, but its type already has a limit.
  2. The limit depends on the size of one node, but on every common 64-bit system it is much larger than 1000000, so huge is true.
  3. The limit and the stored count are unrelated. size() still shows 0.

Example 3 — max_size() as a Safety Check

Write a helper that adds a pair only if the unordered_map is below its limit.

C++
#include <iostream>
#include <unordered_map>
#include <string>

bool addIfRoom(std::unordered_map<int, std::string>& m, int key, const std::string& value) {
    if (m.size() >= m.max_size()) {
        return false;
    }
    m.insert({key, value});
    return true;
}

int main() {
    std::unordered_map<int, std::string> m;
    std::cout << std::boolalpha;

    std::cout << "added 1: " << addIfRoom(m, 1, "one") << '\n';
    std::cout << "added 2: " << addIfRoom(m, 2, "two") << '\n';
    std::cout << "size: " << m.size() << '\n';
    return 0;
}

How It Works

  1. addIfRoom() compares size() with max_size(). If the unordered_map were full it would return false and skip the insert.
  2. The unordered_map is almost empty, so both checks pass and both calls insert a pair and return true.
  3. In real programs this guard never fails. It only shows how max_size() is used as an upper bound, so the final size is 2.

Frequently Asked Questions

The largest number of pairs an unordered_map of that type could theoretically hold. It is a limit of the library and the node size, not of the free memory.
No. Your computer will run out of memory long before that number. If it does, insert() throws std::bad_alloc.
It is computed from the largest memory block the allocator could give, divided by the size of one node. On a 64-bit system that is a number in the billions or more.
No. The value is implementation-defined, so it can differ between compilers and platforms. Do not hard-code it.
size() is the number of pairs in the unordered_map now. max_size() is the most the type could ever hold in theory.

Next: merge()

Keep going through the std::unordered_map functions. Move pairs with new keys from another map 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.

7 people found this page helpful