Facebook Page Twitter Page LinkedIn Page
× C++ Standard Library C++ STL Library

The C++ set::max_size function is used to return the maximum size the set can reach. The function returns the maximum potential size the set can reach due to known system or library implementation limitations.

Note: Set is an ordered data container which implies all its elements are ordered all the time.


size_type max_size() const;
size_type max_size() const noexcept;


No parameter is required.

Return Value

Maximum number of elements that can be held in a set.

Time Complexity

Constant i.e, Θ(1).


In the below example, the set::max_size function is used find out the maximum number of elements that a set can hold.

#include <iostream>
#include <set>
using namespace std;
int main (){
  set<int> MySet{55, 25, 128, 5, 72};
  set<int>::iterator it;

  cout<<"The Set contains:";
  for(it = MySet.begin(); it != MySet.end(); ++it)
    cout<<" "<<*it;
  cout<<"\nSet size is: "<<MySet.size()<<"\n";
  cout<<"Maximum size of the Set: "<<MySet.max_size()<<"\n"; 
  return 0;

A possible output could be:

The Set contains: 5 25 55 72 128
Set size is: 5
Maximum size of the Set: 461168601842738790

❮ C++ - Set