C++ .max_size()
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.
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 elementsstd::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:
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve 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