C++ Array::empty() Function
The C++ std::array::empty() function is used to check whether a array is empty. Since the std::array is a fixed size container, its size is known at compile time, and it cannot be dynamically resized. This function always return false for std::array because array is never empty unless it has zero size(which is rare).
Syntax
Following is the syntax for std::array::empty() function.
constexpr bool empty() noexcept;
Parameters
This does not accepts any parameter.
Return Value
It returns a bool value indicating whether the array is empty or not.
Exceptions
This function never throws exception.
Time complexity
Constant i.e. O(1)
Example 1
In the following example, we are going to consider the basic usage of the empty() function.
#include <iostream>
#include <array>
using namespace std;
int main() {
array < int, 10 > myarray {9,12,15,18,21,24,27,30,33,36};
if (myarray.empty()) {
cout << "True";
} else {
cout << "False";
}
return 0;
}
Output
Output of the above code is as follows −
False
Example 2
Consider the following example, where we are going to consider two array one with size zero and another with size 10.
#include <iostream>
#include <array>
using namespace std;
int main(void) {
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;
}
Output
Following is the output of the above code −
arr1 is empty arr2 is not empty
array.htm