C++ Deque::max_size() Function



The C++ std::deque::max_size() function is used to return the maximum number of elements that a deque container can hold. This value is determined by the system or library implementation and indicates the largest possible size that the container can expand. This function is useful for understanding capacity limits when working with the large datasets.

Syntax

Following is the syntax for std::deque::max_size() function.

size_type max_size() const noexcept;

Parameters

It does not accepts any parameter.

Return value

It returns the maximum numbers of elements a deque can hold.

Exceptions

This function never throws exception.

Time complexity

The time complexity of this function is Constant i.e. O(1)

Example

In the following example, we are going to consider the basic usage of the max_size() function.

#include <iostream>
#include <deque>
int main()
{
    std::deque<int> a;
    std::cout << "Max_size of deque is: " << a.max_size() << std::endl;
    return 0;
}

Output

Following is the output of the above code −

Max_size of deque is: 2305843009213693951

Example

Consider the following examples, where we are going to get the current size and max_size of the deque.

#include <iostream>
#include <deque>
int main()
{
    std::deque<int> a(123);
    std::cout << "Current size: " << a.size() << std::endl;
    std::cout << "Max_size of deque: " << a.max_size() << std::endl;
    return 0;
}

Output

Output of the above code is as follows −

Current size: 123
Max_size of deque: 2305843009213693951
deque.htm