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


The C++ forward_list::max_size function is used to return 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 below example, 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> f_list{10, 20, 30, 40, 50};
  forward_list<int>::iterator it;

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

A possible output could be:

The forward_list contains: 10 20 30 40 50
Maximum size of the Forward List: 1152921504606846975

❮ C++ <forward_list> Library