C++ Array Library - max_size() Function


Advertisements

Description

The C++ function std::array::max_size() is used to get the maximum number of elements that can be held by array container.

Declaration

Following is the declaration for std::array::max_size() function form std::array header.

constexpr size_type max_size() noexcept;

Parameters

None

Return Value

Returns the maximum number of elements that can be held by array container. This value is always same as the second parameter of the array template used to instantiate array.

Exceptions

This member function never throws exception.

Time complexity

Constant i.e. O(1)

Example

The following example shows the usage of std::array::max_size() function.

#include <iostream>
#include <array>

using namespace std;

int main(void) {

   array<int, 10>arr; /* array of 10 integers */

   cout << "maximum size of arr = " << arr.max_size() 
      << endl;

   cout << "size of arr         = " << arr.size() 
      << endl;

   return 0;
}

Let us compile and run the above program, this will produce the following result −

maximum size of arr = 10
size of arr         = 10
array.htm
Advertisements