Facebook Page Twitter Page LinkedIn Page
× C++ Standard Library C++ STL Library

The C++ unordered_map::max_size function is used to return 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.


size_type max_size() const noexcept;


No parameter is required.

Return Value

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

Time Complexity

Constant i.e, Θ(1).


In the below example, 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:
101  John
102  Marry
103  Kim
104  Jo
105  Ramesh

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

❮ C++ <unordered_map> Library