list max_size() function in C++ STL


In this article we will be discussing the working, syntax and examples of list::max_size() function in C++.

What is a List in STL?

List is a data structure that allows constant time insertion and deletion anywhere in sequence. Lists are implemented as doubly linked lists. Lists allow non-contiguous memory allocation. List perform better insertion extraction and moving of element in any position in container than array, vector and deque. In List the direct access to the element is slow and list is similar to forward_list, but forward list objects are single linked lists and they can only be iterated forwards.

What is list::max_size()?

list:: max_size() is an inbuilt function in C++ STL which is declared in header file. max_size() returns the maximum size of the list container. In other words it returns the maximum size that a container can reach, however there is no guarantee it can allocate the elements of that size, it can still fail to allocate the storage to a specific point of a list container.

Syntax

list_container.max_size()

This function accepts no parameter.

Return Value

This function returns a size_type value i.e. maximum size of list_container.

Example

In the below code we will call the max_size function to check the maximum size of the list which can be possible.

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main(){
   //Create a list
   list<int> myList;
   //call max_size for the maximum size
   cout<<"maximum size of a list is : "<<myList.max_size();
   return 0;
}

Output

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

maximum size of a list is : 768614336404564650

Updated on: 02-Mar-2020

227 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements