C++ map max_size function is used to get the maximum size a map container can hold.
Syntax
Member type size_type is an unsigned integral type.
size_type max_size() const; // until C++ 11
size_type max_size() const noexcept; //since C++ 11
Parameter
Return value
It returns the maximum allowed length of the map container.
Example 1
Let's see a simple example to calculate the maximum size of the map.
#include <iostream>
#include <map>
using namespace std;
int main()
{
map<char,char> s;
cout << "Maximum size of a 'map' is " << s.max_size() << "\n";
}
Output:
Maximum size of a 'map' is 461168601842738790
In the above example, max_size function returns the maximum size of the map.
Example 2
Let's see a simple example.
#include <iostream>
#include <map>
using namespace std;
int main ()
{
int i;
map<int,int> mymap;
if (mymap.max_size()>1000)
{
for (i=0; i<1000; i++) mymap[i]=0;
cout << "The map contains 1000 elements.\n";
}
else cout << "The map could not hold 1000 elements.\n";
return 0;
}
Output:
The map contains 1000 elements.
In the above example, member max_size is used to check beforehand whether the map will allow for 1000 elements to be inserted.
Example 3
Let's see a simple example to find the max size of an empty map and a non-empty map.
#include <map>
#include <iostream>
using namespace std;
int main()
{
// initialize container
map<int, int> mp1, mp2;
mp1[1] = 1111;
// max size of Non-empty map
cout << "The max size of mp1 is " << mp1.max_size();
// max size of Empty-map
cout << "\nThe max size of mp2 is " << mp2.max_size();
return 0;
}
Output:
The max size of mp1 is 461168601842738790
The max size of mp2 is 461168601842738790
In the above example, there are two maps i.e. m1 and m2. m1 is a non-empty map and m2 is an empty map. But the maximum size of both maps is the same.
Example 4
Let's see a simple example.
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main() {
typedef map<string, int> city;
string name;
int age;
city fmly ;
int n;
cout<<"Enter the number of fmly members :";
cin>>n;
cout<<"Enter the name and age of each member: \n";
for(int i =0; i<n; i++)
{
cin>> name; // Get key
cin>> age; // Get value
fmly[name] = age; // Put them in map
}
cout<<"\nTotal number of population of city map: "<<fmly.max_size();
cout<<"\nTotal memnber of fmly is:"<< fmly.size();
cout<<"\nDetails of fmly members: \n";
cout<<"\nName | Age \n ________________________\n";
city::iterator p;
for(p = fmly.begin(); p!=fmly.end(); p++)
{
cout<<(*p).first << " | " <<(*p).second <<" \n ";
}
return 0;
}
Output:
Enter the number of fmly members : 3
Enter the name and age of each member:
Ram 42
Sita 37
Laxman 40
Total number of population of city map: 384307168202282325
Total memnber of fmly is:3
Details of fmly members:
Name | Age
__________________________
Laxman | 40
Ram | 42
Sita | 37
In the above example, the program first creates city map interactively with given number of size. Then, it displays the total size a city map can contain, total size of a fmly and all the names and their age available in the map.