C++ Standard Library C++ STL Library

C++ <forward_list> - max_size() Function



The C++ forward_list::max_size function returns the maximum number of elements the forward_list can hold. The function returns the maximum potential size the forward_list can reach due to known system or library implementation limitations.

Syntax

size_type max_size() const noexcept;

Parameters

No parameter is required.

Return Value

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

Time Complexity

Constant i.e, Θ(1).

Example:

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

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

  cout<<"The flist contains:";
  for(it = flist.begin(); it != flist.end(); ++it)
    cout<<" "<<*it;
    
  cout<<"\nMaximum size of the Forward List: "<<flist.max_size()<<"\n"; 
  return 0;
}

A possible output could be:

The flist contains: 10 20 30 40 50
Maximum size of the Forward List: 576460752303423487

❮ C++ <forward_list> Library