C++ Array Library - empty() Function



Description

The C++ function std::array::empty() tests whether size of array is zero or not.

Declaration

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

constexpr bool empty() noexcept;

Parameters

None

Return Value

Returns true if array size is 0 otherwise false.

Exceptions

This member function never throws exception.

Time complexity

Constant i.e. O(1)

Example

In below example size of arr1 is 0 that is why it will be treated as empty array and member function will return true value for arr1.

#include <iostream>
#include <array>

using namespace std;

int main(void) {
   
   /* array size is zero, it will be treated as empty array */
   array<int, 0> arr1;   
   array<int, 10> arr2;

   if (arr1.empty())
      cout << "arr1 is empty" << endl;
   else
      cout << "arr1 is not empty" << endl;

   if (arr2.empty())
      cout << "arr2 is empty" << endl;
   else
      cout << "arr2 is not empty" << endl;
}

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

arr1 is empty
arr2 is not empty
array.htm
Advertisements