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

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


size_type max_size() const noexcept;


No parameter is required.

Return Value

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

Time Complexity

Constant i.e, Θ(1).


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

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

  cout<<"The Unordered Set contains:";
  for(it = uSet.begin(); it != uSet.end(); ++it)
    cout<<" "<<*it;

  cout<<"\nUnordered Set size is: "<<uSet.size()<<"\n";
  cout<<"Maximum size of the Unordered Set: "<<uSet.max_size()<<"\n"; 
  return 0;

A possible output could be:

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

❮ C++ - Unordered Set