Skip to content
Related Articles

Related Articles

Improve Article

array::max_size() in C++ STL

  • Last Updated : 30 Jan, 2018
Geek Week

Array classes are generally more efficient, light-weight and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays.

array::max_size()

This function returns the maximum number of elements that the array container can contain.
In case of an array, size() and max_size() function always return the same value

Syntax :

arrayname.max_size()
Parameters :
No parameter is passed.
Returns :
It returns the maximum number of
elements that the array can contain.

Examples:

Input  : myarray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
         myarray.max_size();
Output : 10

Input  : myarray = {1, 2, 3, 4, 5};
         myarray.max_size();
Output : 5

Errors and Exceptions

1. It throws an error if a parameter is passed.
2. It has a no exception throw guarantee otherwise.




// CPP program to illustrate
// Implementation of max_size() function
#include <array>
#include <iostream>
using namespace std;
  
int main()
{
    array<int, 5> myarray{ 1, 2, 3, 4, 5 };
    cout << myarray.max_size();
    return 0;
}

Output:

5
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :