C++ std::unordered_set max_load_factor() Function

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

Definition

In C++, std::unordered_set::max_load_factor() is the target ceiling for load_factor().

Call it with no argument to read the limit, or pass a new float to change it. 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

Read with empty brackets, or pass a new maximum.

C++
float lim = m.max_load_factor();   // read
m.max_load_factor(0.5f);           // set a lower ceiling
CallWhat it does
m.max_load_factor()Returns the current maximum load factor.
m.max_load_factor(z)Sets the maximum load factor to z. May rehash.

The default is often 1.0. Lowering it uses more buckets (and memory) for fewer collisions. Raising it saves memory but can slow lookups.

Library declarations
C++
float max_load_factor() const;
void max_load_factor(float z);

Parameters

ParameterDescription
zNew maximum load factor when using the setter. Must be positive.

Return Value

Return ValueDescription
floatFor the getter: the current maximum load factor.
voidFor the setter: nothing. The table may rehash to satisfy the new limit.

C++ Examples

Example 1 — Default max_load_factor()

Print the default maximum load factor.

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

int main() {
    std::unordered_set<int> m;
    std::cout << "max_load_factor: " << m.max_load_factor() << '\n';
    return 0;
}

How It Works

  1. A fresh unordered_set is created.
  2. max_load_factor() reads the library default.
  3. Many implementations use 1.0, meaning about one element per bucket on average before growth.

Example 2 — Set a Smaller Max Load Factor

Lower the ceiling and show that bucket_count can rise.

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

int main() {
    std::unordered_set<int> m;
    for (int i = 0; i < 8; ++i) m[i] = i;

    std::size_t before = m.bucket_count();
    m.max_load_factor(0.25f);
    std::size_t after = m.bucket_count();

    std::cout << "before buckets: " << before << '\n';
    std::cout << "after buckets: " << after << '\n';
    std::cout << "load_factor: " << m.load_factor() << '\n';
    return 0;
}

How It Works

  1. Eight keys are inserted under the default max load factor.
  2. Setting max_load_factor(0.25f) asks for a sparser table, so the library rehashes.
  3. Bucket count rises and the new load factor sits under the new ceiling.

Example 3 — Stay Under the Maximum

Show load_factor stays at or below max_load_factor after inserts.

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

int main() {
    std::unordered_set<int, char> m;
    for (int i = 0; i < 50; ++i) {
        m[i] = 'a';
    }

    std::cout << std::boolalpha;
    std::cout << "load: " << m.load_factor() << '\n';
    std::cout << "max: " << m.max_load_factor() << '\n';
    std::cout << "load <= max: "
              << (m.load_factor() <= m.max_load_factor()) << '\n';
    return 0;
}

How It Works

  1. Fifty inserts may cause automatic rehashes along the way.
  2. Afterward, load_factor() and max_load_factor() are printed.
  3. The boolean check is true: the container keeps the average at or under its maximum.

Frequently Asked Questions

It gets or sets the maximum average elements-per-bucket the container tries to keep.
Often 1.0, but check your library.
It may rehash if the current load factor is already above the new limit.
Usually no. Prefer reserve(n) when you know how many keys you will insert.
load_factor is the current average. max_load_factor is the limit that triggers growth.

Next: max_size()

Keep going through the std::unordered_set functions. Get some number of values an unordered_set could ever hold.

max_size() 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.

13 people found this page helpful