C++ Unordered_map Library - load_factor() Function



Description

The C++ function std::unordered_map::load_factor() Returns the current load factor of the unordered_map container.

The load factor is calculated as follows −

load_factor = um.size() / um.bucket_count()

Declaration

Following is the declaration for std::unordered_map::load_factor() function form std::unordered_map header.

C++11

float load_factor() const noexcept;

Parameters

None

Return value

Returns load factor

Exceptions

This member function never throws exception.

Time complexity

Constant i.e.O(1)

Example

The following example shows the usage of std::unordered_map::load_factor() function.

#include <iostream>
#include <unordered_map>

using namespace std;

int main(void) {
   unordered_map<char, int> um;

   cout << "load_factor of unordered_map = " << um.load_factor() << endl;

   return 0;
}

Let us compile and run the above program, this will produce the following result −

load_factor of unordered_map = 0
unordered_map.htm
Advertisements