C++ Standard Library C++ STL Library

C++ <array> - size() Function



The C++ array::size function is used to find out the total number of elements in the array. It returns the total number of elements held in the array, and it is not necessarily equal to its storage capacity.

Syntax

size_type size() const noexcept;

Parameters

No parameter is required.

Return Value

Number of elements present in the array.

Time Complexity

Constant i.e, Θ(1).

Example:

In the example below, the array::size function is used to find out the total number of elements in an array called Arr.

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

  cout<<"Array size is: "<<Arr.size();
  return 0;
}

The output of the above code will be:

Array size is: 100

❮ C++ <array> Library