C++ Standard Library C++ STL Library

C++ <array> - empty() Function



The C++ array::empty function is used to check whether the array is empty or not. It returns true if the size of the array is zero, else returns false.

Syntax

bool empty() const noexcept;

Parameters

No parameter is required.

Return Value

true if the size of the array is zero, else returns false.

Time Complexity

Constant i.e, Θ(1).

Example:

In the example below, the array::empty function is used to check whether the array is empty or not.

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

  cout<<boolalpha;
  
  cout<<"Is the Array empty?: "<<MyArray.empty()<<"\n";

  cout<<"Add elements in the Array.\n";
  MyArray[0] = 10;
  MyArray[1] = 20;
  MyArray[2] = 30;

  cout<<"Now, Is the Array empty?: "<<MyArray.empty()<<"\n";
  return 0;
}

The output of the above code will be:

Is the Array empty?: false
Add elements in the Array.
Now, Is the Array empty?: false

❮ C++ <array> Library