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

Set Max Size Function

BLUF: Mastering Set 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: Set Max Size Function

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

C++ set max_size

The max_size function in C++ is utilized to determine the maximum capacity that a set container can accommodate.

Syntax

The size_type member type is an unsigned integral data type.

Example

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

Parameter

Return value

It provides the maximum permissible size of the set container.

Complexity

Constant.

Iterator validity

No changes.

Data Races

The container is accessed.

Simultaneously retrieving the elements of a set is considered secure.

Exception Safety

This member function never throws exception.

Example 1

Let's explore a straightforward example to determine the maximum size of a set:

Example

#include <iostream>
#include <set>

using namespace std;
 
int main()
{
    set<char,char> s;
    cout << "Maximum size of a 'set' is " << s.max_size() << "\n";
    return 0;
}

Output:

Output

Maximum size of a 'set' is 461168601842738790

In the previous example, the max_size method retrieves the largest size of the set.

Example 2

Let's see a simple example:

Example

#include <iostream>
#include <set>

using namespace std;

int main ()
{
  int i;
  set<int> myset;

  if (myset.max_size()>1000)
  {
    for (i=0; i<1000; i++) myset.insert(0);
    cout << "The set contains 1000 elements.\n";
  }
  else cout << "The set could not hold 1000 elements.\n";

  return 0;
}

Output:

Output

The set contains 1000 elements.

In the provided example, the member max_size is utilized to verify in advance if the set can accommodate the insertion of 1000 elements.

Example 3

Let's explore a basic illustration to determine the maximum size of a vacant set and a populated set:

Example

#include <set>
#include <iostream>

using namespace std;
 
int main()
{
 
    // initialize container
    set<int> mp1, mp2;
    mp1 = {1111};
 
    // max size of Non-empty set
    cout << "The max size of mp1 is " << mp1.max_size();
 
    // max size of Empty-set
    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 example provided, there are two collections known as m1 and m2. The set m1 contains elements, while set m2 is devoid of any elements. Despite this distinction, both sets have an equivalent maximum capacity.

Example 4

Let's see a simple example:

Example

#include <iostream>
#include <set>
#include <string>
using namespace std;

int main() {

  typedef set<string> city;  
   string name;
   city fmly ;
   int n;

   cout<<"Enter the number of family members :";
   cin>>n;

   cout<<"Enter the name of each member: \n";
   for(int i =0; i<n; i++)
   {
       cin>> name;      // Get key
       fmly.insert(name);   // Put them in set
   }
   
      cout<<"\nTotal number of population of city set: "<<fmly.max_size();
      cout<<"\nTotal member of family is:"<< fmly.size();

      cout<<"\nName of family members: \n";
      cout<<"\nName \n ________________________\n";
      city::iterator p;
      for(p = fmly.begin(); p!=fmly.end(); p++)
      {
          cout<<(*p)<<" \n ";
      }
    
   return 0;
}

Output:

Output

Enter the number of family members: 5
Enter the name of each member: 
Aman
Nikita
Divya
Amita
Kashish

Total number of population of city set: 461168601842738790
Total member of family is:5
Name of family members: 

Name 
 ________________________
Aman 
 Amita 
 Divya 
 Kashish 
 Nikita

In the previous example, the software initially generates a city set interactively with a specified size. Subsequently, it showcases the maximum capacity of a city set, the total size of a family, and the complete list of names along with their corresponding ages stored within the set.

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