Home C++ Unordered Maps Functions max_load_factor() C++ std::unordered_map max_load_factor() Function Definition In C++, std::unordered_map::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_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 Read with empty brackets, or pass a new maximum.
float lim = m.max_load_factor(); // read
m.max_load_factor(0.5f); // set a lower ceiling Call What 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 float max_load_factor() const;
void max_load_factor(float z); Parameters Parameter Description zNew maximum load factor when using the setter. Must be positive.
Return Value Return Value Description floatFor the getter: the current maximum load factor. voidFor the setter: nothing. The table may rehash to satisfy the new limit.
C++ Examples 1. Read the default limit 2. Lower the maximum 3. Compare load and max Example 1 — Default max_load_factor() Print the default maximum load factor.
#include <iostream>
#include <unordered_map>
int main() {
std::unordered_map<int, int> m;
std::cout << "max_load_factor: " << m.max_load_factor() << '\n';
return 0;
} How It Works A fresh unordered_map is created. max_load_factor() reads the library default.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.
#include <iostream>
#include <unordered_map>
int main() {
std::unordered_map<int, 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;
} before buckets: 11
after buckets: 37
load_factor: 0.216216 How It Works Eight keys are inserted under the default max load factor. Setting max_load_factor(0.25f) asks for a sparser table, so the library rehashes. 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.
#include <iostream>
#include <unordered_map>
int main() {
std::unordered_map<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;
} load: 0.485437
max: 1
load <= max: true How It Works Fifty inserts may cause automatic rehashes along the way. Afterward, load_factor() and max_load_factor() are printed. The boolean check is true: the container keeps the average at or under its maximum. Frequently Asked Questions What does max_load_factor() do? It gets or sets the maximum average elements-per-bucket the container tries to keep.
What is a typical default? Often 1.0, but check your library.
Does setting max_load_factor always rehash? It may rehash if the current load factor is already above the new limit.
Should beginners change it? Usually no. Prefer reserve(n) when you know how many keys you will insert.
What is the difference between load_factor and max_load_factor? load_factor is the current average. max_load_factor is the limit that triggers growth.
Next: max_size() Keep going through the std::unordered_map functions. Get the largest number of pairs the unordered_map could ever hold.
max_size() tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
15 people found this page helpful
Helpful Share Copy link Suggestion