In C++, std::array::empty() returns true when the array has no elements and false when it has at least one.
A std::array<T, N> has a fixed size N, so the answer never changes while the program runs. For N > 0 it is usually false. It is true only for std::array<T, 0>. Include <array> to use it.
This is a member of std::array<T, N>, not of a plain C array and not of std::vector. A std::array has a fixed size N that is part of its type, and it lives on the stack like a normal variable.
Syntax
Call empty with empty brackets. It takes no arguments.
C++
bool none = nums.empty();
if (!nums.empty()) { /* safe to read */ }
Call
What it does
nums.empty()
Returns true if the array has zero elements.
!nums.empty()
Returns true if the array has at least one element.
empty() asks about the size, not the values. An array full of zeros is not empty.
Library declarations
C++
constexpr bool empty() const noexcept;
Parameters
Parameter
Description
None
empty() takes no parameters. Call it with empty brackets.
Return Value
Return Value
Description
true
The array has no elements. This only happens for std::array<T, 0>.
false
The array has one or more elements.
C++ Examples
Example 1 — Basic empty()
Compare an array of 3 ints with an array of 0 ints.