C++ std::unordered_map load_factor() Function

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

Definition

In C++, std::unordered_map::load_factor() returns the average number of elements per bucket: size() / bucket_count().

A low load factor means keys are spread out. When the load factor grows past max_load_factor(), the table usually grows. 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 load_factor with empty brackets. It returns a float.

C++
float lf = m.load_factor();
std::cout << lf;
CallWhat it does
m.load_factor()Returns size() / bucket_count() as a float (empty table is defined by the library, often 0).

Watch load factor when you care about speed. If it stays near the maximum, expect more collisions until a rehash happens.

Library declarations
C++
float load_factor() const;

Parameters

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

Return Value

Return ValueDescription
floatAverage elements per bucket.

C++ Examples

Example 1 — Print load_factor()

Show size, buckets, and load factor together.

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

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

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

How It Works

  1. Three pairs are stored.
  2. The table has more buckets than keys, so the average per bucket is well below 1.
  3. load_factor() prints that average. Exact digits can vary slightly by library.

Example 2 — Load Factor When Size Is Zero

Check the load factor of an empty unordered_map.

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

int main() {
    std::unordered_map<int, int> m;
    std::cout << "size: " << m.size() << '\n';
    std::cout << "buckets: " << m.bucket_count() << '\n';
    std::cout << "load_factor: " << m.load_factor() << '\n';
    return 0;
}

How It Works

  1. No pairs are stored yet.
  2. The library still reports a bucket count for the empty table.
  3. With size zero, the load factor is 0.

Example 3 — Reserve Lowers Load Factor Pressure

Compare load factor before and after reserve while size stays the same.

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

int main() {
    std::unordered_map<int, int> m;
    for (int i = 0; i < 10; ++i) m[i] = i;

    float before = m.load_factor();
    m.reserve(100);
    float after = m.load_factor();

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

How It Works

  1. Ten keys are inserted first, so the table has a modest load factor.
  2. reserve(100) grows the bucket array to prepare for about 100 elements.
  3. With the same size(), more buckets mean a smaller load factor after the reserve.

Frequently Asked Questions

size() divided by bucket_count(): the average number of elements per bucket.
Usually yes for lookups, because collisions are rarer. Extremely low values waste memory.
The container typically rehashes into more buckets once it would exceed max_load_factor().
No. It is only an average. Check bucket_size() for hot spots.
float.

Next: max_bucket_count()

Keep going through the std::unordered_map functions. The largest number of buckets this unordered_map type could have.

max_bucket_count() 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