Map Max Size Function - C++ Programming Tutorial
C++ Course / STL Set & Map / Map Max Size Function

Map Max Size Function

BLUF: Mastering Map Max Size Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Map Max Size Function

C++ is renowned for its efficiency. Learn how Map Max Size Function enables low-level control and high-performance computing in the tutorial below.

The max_size function in C++ map is utilized to retrieve the maximum capacity that a map container is capable of holding.

Syntax

The type size_type, as a member, is an integral type that is unsigned.

Example

size_type max_size() const; // until C++ 11
size_type max_size() const noexcept; //since C++ 11

Parameter

Return value

It provides the highest permissible length of the map container.

Example 1

Let's examine a basic illustration to determine the maximum capacity of the map.

Example

#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:

Output

Maximum size of a 'map' is 461168601842738790

In the aforementioned example, the max_size method provides the maximum capacity of the map.

Example 2

Let's see a simple example.

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:

Output

The map contains 1000 elements.

In the provided instance, the member max_size is employed to verify in advance if the map will accommodate the insertion of 1000 elements.

Example 3

Let's examine a straightforward illustration to determine the maximum capacity of both an empty map and a map with existing elements.

Example

#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:

Output

The max size of mp1 is 461168601842738790
The max size of mp2 is 461168601842738790

In the previous illustration, there are two maps: m1 and m2. m1 contains elements while m2 is devoid of any entries. However, both maps have an identical maximum capacity.

Example 4

Let's see a simple example.

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:

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 provided example, the software initially generates a city map interactively based on the specified size. Subsequently, it showcases the maximum capacity of the city map, the overall capacity for a family, and a comprehensive list of names along with their respective ages present within the map.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience