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


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

Note: Map is an ordered data container which implies all its elements are ordered all the time.

Syntax

size_type max_size() const;
size_type max_size() const noexcept;

Parameters

No parameter is required.

Return Value

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

Time Complexity

Constant i.e, Θ(1).

Example:

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

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

  MyMap[101] = "John";
  MyMap[102] = "Marry";
  MyMap[103] = "Kim";
  MyMap[104] = "Jo";
  MyMap[105] = "Ramesh";
  cout<<"The map contains:\n";
  for(it = MyMap.begin(); it != MyMap.end(); ++it)
     cout<<it->first<<"  "<<it->second<<"\n";

  cout<<"\nMap size is: "<<MyMap.size()<<"\n";
  cout<<"Maximum size of the Map: "<<MyMap.max_size()<<"\n"; 
  
  return 0;
}

A possible output could be:

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

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

❮ C++ - Map