forward_list max_size() in C++ STL with Examples

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

The forward_list elements: 8 8 8 8 8 
The max size: 1152921504606846975

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

The max size: 1152921504606846975

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.