map max_size() in C++ STL

C++Server Side ProgrammingProgramming

In this article we will be discussing the working, syntax and examples of map::max_size() function in C++ STL.

What is a Map in C++ STL?

Maps are the associative container, which facilitates to store the elements formed by a combination of key value and mapped value in a specific order. In a map container the data is internally always sorted with the help of its associated keys. The values in the map container are accessed by its unique keys.

What is a map::max_size()?

map::max_size() function is an inbuilt function in C++ STL, which is defined in <map> header file. max_size() is used to return the maximum size of the map container.

This function is used to check the maximum number of values that a map container can hold. The size is like the potential of the container, hence there is no guarantee that it can reach that value or not.




This function accepts no parameter.

Return value

This function returns the number of elements a container can hold.


map<char, int> newmap;


Max size of map is: 461168601842738790


 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main(){
   map<int, int> TP_1, TP_2;
   TP_1.insert({ 0, 10 });
   cout<<"Max size of map with elements is: " << TP_1.max_size();
   cout<<"\Max size of map without elements is: " << TP_2.max_size();
   return 0;


If we run the above code it will generate the following output −

Max size of map with elements is: 461168601842738790
Max size of map without elements is: 461168601842738790
Published on 14-Aug-2020 11:53:58