C++ Standard Library C++ STL Library

C++ <array> - max_size() Function



The C++ array::max_size function returns the maximum size the array can reach.

Syntax

size_type max_size() const noexcept;

Parameters

No parameter is required.

Return Value

Maximum number of elements that can be held in an array.

Time Complexity

Constant i.e, Θ(1).

Example:

In the example below, the array::max_size function is used to find out the maximum number of elements that an array can hold.

#include <iostream>
#include <array>
using namespace std;
 
int main (){
  array<int, 100> MyArray;

  cout<<"Array size is: "<<MyArray.size()<<"\n";
  cout<<"Maximum size of the Array: "<<MyArray.max_size()<<"\n"; 
  return 0;
}

A possible output could be:

Array size is: 100
Maximum size of the Array: 100

❮ C++ <array> Library