In C++, std::array::max_size() returns the largest number of elements the array can ever hold. For a std::array<T, N> that number is simply N.
That makes it the same as size() for arrays. The function exists because all standard containers have it. Include <array> to use it.
This is a member of std::array<T, N>, not of a plain C array and not of std::vector. A std::array has a fixed size N that is part of its type, and it lives on the stack like a normal variable.
Syntax
Call max_size with empty brackets. It takes no arguments.
C++
std::size_t limit = nums.max_size();
Call
What it does
nums.max_size()
Returns the most elements this array can hold. For std::array<int, 5> that is 5.
nums.max_size() == nums.size()
Always true for a std::array.
For a std::vector, max_size() is a huge number. For std::array it is just N.
Library declarations
C++
constexpr size_type max_size() const noexcept;
Parameters
Parameter
Description
None
max_size() takes no parameters. Call it with empty brackets.
Return Value
Return Value
Description
Size
The value N from std::array<T, N>, of type size_type.