C++ Standard Library C++ STL Library

C++ multimap - max_size() Function



The C++ multimap::max_size function returns 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.

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 multimap.

Time Complexity

Constant i.e, Θ(1).

Example:

In the example below, 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: 96076792050570581

❮ C++ <map> Library