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

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

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


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


No parameter is required.

Return Value

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

Time Complexity

Constant i.e, Θ(1).


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

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

  MyMMap.insert(pair<string, string>("USA", "New York"));
  MyMMap.insert(pair<string, string>("USA", "Washington"));  
  MyMMap.insert(pair<string, string>("CAN", "Toronto"));
  MyMMap.insert(pair<string, string>("CAN", "Montreal"));
  MyMMap.insert(pair<string, string>("IND", "Delhi"));

  cout<<"The multimap contains:\n";
  for(it = MyMMap.begin(); it != MyMMap.end(); ++it)
     cout<<it->first<<"  "<<it->second<<"\n";
  cout<<"\nMultimap size is: "<<MyMMap.size()<<"\n";
  cout<<"Maximum size of the Multimap: "<<MyMMap.max_size()<<"\n"; 
  return 0;

A possible output could be:

The multimap contains:
CAN  Toronto
CAN  Montreal
IND  Delhi
USA  New York
USA  Washington

Multimap size is: 5
Maximum size of the Multimap: 384307168202282325

❮ C++ - Multimap