Skip to content
Related Articles

Related Articles

Improve Article

forward_list max_size() in C++ STL with Examples

  • Difficulty Level : Basic
  • Last Updated : 23 Jul, 2018

The forward_list::max_size() is a built-in function in C++ STL which returns the maximum number of elements a forward_list container can hold

Syntax:

forward_list_name.max_size()

Parameters: The function does not accept any parameters.

Return Value: The function returns the maximum number of elements the container can hold.

Below programs illustrate the above function:



Program 1:




// CPP program to demonstrate the
// forward_list::max_size() function
// when the list is not-empty
#include <bits/stdc++.h>
using namespace std;
int main()
{
    // declaration of forward list
    forward_list<int> fl;
  
    // assign value
    fl.assign(5, 8);
  
    // prints the elements
    cout << "The forward_list elements: ";
  
    for (auto it = fl.begin(); it != fl.end(); it++)
        cout << *it << " ";
  
    cout << "\nThe max size: " << fl.max_size();
    return 0;
}
Output:
The forward_list elements: 8 8 8 8 8 
The max size: 1152921504606846975

Program 1:




// CPP program to demonstrate the
// forward_list::max_size() function
// when the list is empty
#include <bits/stdc++.h>
using namespace std;
int main()
{
    // declaration of forward list
    forward_list<int> fl;
  
    cout << "\nThe max size: " << fl.max_size();
    return 0;
}
Output:
The max size: 1152921504606846975
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :