C++ Standard Library C++ STL Library

C++ unordered_map - max_size() Function



The C++ unordered_map::max_size function returns the maximum size the unordered_map can reach. The function returns the maximum potential size the unordered_map can reach due to known system or library implementation limitations.

Syntax

size_type max_size() const noexcept;

Parameters

No parameter is required.

Return Value

Maximum number of elements that can be held in a unordered_map.

Time Complexity

Constant i.e, Θ(1).

Example:

In the example below, the unordered_map::max_size function is used find out the maximum number of elements that a unordered_map can hold.

#include <iostream>
#include <unordered_map>
using namespace std;
 
int main (){
  unordered_map<int, string> uMap;
  unordered_map<int, string>::iterator it;

  uMap[101] = "John";
  uMap[102] = "Marry";
  uMap[103] = "Kim";
  uMap[104] = "Jo";
  uMap[105] = "Ramesh";
  cout<<"The unordered_map contains:\n";
  for(it = uMap.begin(); it != uMap.end(); ++it)
     cout<<it->first<<"  "<<it->second<<"\n";
  
  cout<<"\nUnordered Map size is: "<<uMap.size()<<"\n";
  cout<<"Maximum size of the Unordered Map: "<<uMap.max_size()<<"\n"; 
  
  return 0;
}

A possible output could be:

The unordered_map contains:
105  Ramesh
104  Jo
103  Kim
102  Marry
101  John

Unordered Map size is: 5
Maximum size of the Unordered Map: 192153584101141162

❮ C++ <unordered_map> Library