C++ Standard Library C++ STL Library

C++ unordered_set - max_size() Function

The C++ unordered_set::max_size function returns 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 example below, 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: 72 128 5 25 55
Unordered Set size is: 5
Maximum size of the Unordered Set: 576460752303423487

❮ C++ <unordered_set> Library