In C++, std::unordered_set::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_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
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.