C++ Standard Library C++ STL Library

C++ <list> - max_size() Function

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


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 list.

Time Complexity

Constant i.e, Θ(1)


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

#include <iostream>
#include <list>
using namespace std;
int main (){
  list<int> MyList{10, 20, 30, 40, 50};
  list<int>::iterator it;

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

A possible output could be:

The list contains: 10 20 30 40 50
List size is: 5
Maximum size of the List: 384307168202282325

❮ C++ <list> Library