forward_list max_size() in C++ STL with Examples


Given is the task to show the working of forward_list max_size() function in C++ STL.

What is a Forward List?

Forward list can be understood as a singly linked list where the tracking can be done only in forward direction but not in a backward direction whereas in the list we can track the elements in both the direction i.e. the element holds the two links one is for forward element and another one is for backward element. Forward lists are therefore fast because they have to hold only one link which will be of a forward element. Forward elements can be inserted and deleted in a constant time.

What is a forward_list max_size() function?

forward_list::reverse( ) is a function in C++ Standard Template Library(STL) that is used to reverse the order of the elements present in the forward list.

Syntax

forwardlist_name.reverse( )

Parameter

This function doesn’t have any parameter.

Return Value

This function doesn’t have any return value. It only performs operation of reversing the list

For Example

Input-: List of elements are: 57 99 54 34 84
Output–: Reversed elements of list are: 84 34 54 99 57
Input-: List of elements are: 40 30 60 90 70
Output–: Reversed elements of list are: 70 90 60 30 40

Approach that is used in the below program is as follows

  • First initialize the list

  • Then we will print the forward list before applying reverse () function.

  • Then we defines the forward.reverse( ) function present in a header file in C++.

  • Then we will display the reversed forward list

Example

/* In the below code we are creating a forward list and inserting elements to the list. Now, the task is to check the size of a forward list after inserting the elements using max_size() function */

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main() {
   //creating forward list
   forward_list<int> myForwardList;
   //add values to forward list
   myForwardList.assign(3, 2);
   cout << "The elements in my forward list are : ";
   for (auto i=myForwardList.begin(); i!=myForwardList.end();i++)
      cout << *i << " ";
   cout << "\nThe size of my Forward List is: " << myForwardList.max_size();
   return 0;
}

Output

If we run the above code it will generate the following output

The elements in my forward list are : 2 2 2
The size of my Forward List is:
1152921504606846975

Example

/* In the below code we are creating a forward list. Now, the task is to check the size of a forward list using max_size() function. */

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main() {
   // creating forward list
   forward_list<int> myForwardList;
   cout << "\nsize of my forward list is: "<<myForwardList.max_size();
   return 0;
}

Output

If we run the above code it will generate the following output

size of my forward list is: 1152921504606846975

Updated on: 02-Mar-2020

72 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements