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;
Call
What 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
Parameter
Description
None
load_factor() takes no parameters. Call it with empty brackets.