C++ .max_size()

Anonymous contributor's avatar
Anonymous contributor
Published Oct 29, 2025
Contribute to Docs

In C++, the .max_size() method returns the maximum number of elements a deque can hold. This value reflects the maximum possible size based on system or library constraints. However, it does not guarantee that the container can actually grow to that size—memory allocation may still fail before reaching the reported limit.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
    • Beginner Friendly.
      11 hours

Syntax

dequeName.max_size();

Parameters:

The method does not take any parameters.

Return value:

Returns the maximum number of elements the deque can potentially hold as a value of type size_type.

Example

In this example, the maximum possible number of elements a deque can hold on the system is retrieved:

#include <iostream>
#include <deque>
int main() {
std::deque<int> mydeque;
// Display the maximum possible number of elements
std::cout << "Maximum possible elements: " << mydeque.max_size() << '\n';
}

Example output on a 64-bit system:

Maximum possible elements: 4611686018427387903

Codebyte Example

In this example, a requested size is compared with the maximum possible size of a deque, and the container is resized only if the size is allowed:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn C++ on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
    • Beginner Friendly.
      11 hours