C++ List::max_size() Function



The C++ std::list::max_size() function is used to retrieve the maximum size of the list.

It returns the maximum number of elements( or max size) that can be held by the current list. In other words, it retrieves the maximum size that a container can reach, however, there is no guarantee it can allocate the elements of that size, and it can still fail to allocate the storage to a specific point of a list container. This value depends on the system or library implementation.

Syntax

Following is the syntax of the C++ std::list::max_size() function −

size_type max_size();

Parameters

  • It does not accept any parameter.

Return Value

This function returns the maximum numbers that can be fit into list.

Example 1

If the list is an int-type.

In the following program, we are using the C++ std::list::max_size() function to get the max size of the current list named num_list.

#include<iostream>
#include<list>
using namespace std;

int main() {
   //create a list
   list<int> num_list;
   cout<<"Size of the list: "<<num_list.size()<<endl;
   cout<<"The max_size of list = "<<num_list.max_size()<<endl;
   return 0;
}

Output

On executing the above program, it will produce the following output −

Size of the list: 0
The max_size of list = 384307168202282325

Example 2

Find the max size of the char-type list.

Following is another example of the C++ std::list::max_size() function. Here, we are creating a list(type-char) named char_list with the values {'a', 'b', 'c', 'd'}. Then, using the max_size() function, we are trying to find the max size of the current list.

#include<iostream>
#include<list>
using namespace std;

int main() {
   //create a list
   list<char> char_list = {'a', 'b', 'c', 'd'};
   cout<<"Size of the list: "<<char_list.size()<<endl;
   cout<<"List elements are: "<<endl;
   for(char c: char_list) {
      cout<<c<<" ";
   }
   cout<<"\nThe max_size of list = "<<char_list.max_size()<<endl;
   return 0;
}

Output

Following is the output of the above program −

Size of the list: 4
List elements are: 
a b c d 
The max_size of list = 384307168202282325

Example 3

Apart from the int-type and char-type list, you can also find the max size of the string-type list.

In this example, we are creating a list(type string) named str_list with the values {"Java", "C++", "Python", "Apex"}. Then, using the max_size() function, we are trying to find the maximum size of this list.

#include<iostream>
#include<list>
using namespace std;

int main() {
   //create a list
   list<string> str_list = {"Java", "C++", "Python", "Apex"};
   cout<<"Size of the list: "<<str_list.size()<<endl;
   cout<<"List elements are: "<<endl;
   for(string s: str_list) {
      cout<<s<<" ";
   }
   cout<<"\nThe max_size of list = "<<str_list.max_size()<<endl;
   return 0;
}

Output

This will generate the following output −

Size of the list: 4
List elements are: 
Java C++ Python Apex 
The max_size of list = 192153584101141162
Advertisements