C++ Array 库 - empty() 函数
描述
C++ 函数 std::array::empty() 测试数组的大小是否为零。
声明
以下是 std::array::empty() 函数形式 std::array 头的声明。
constexpr bool empty() noexcept;
参数
None
返回值
如果数组大小为 0,则返回 true,否则返回 false。
异常
此成员函数从不抛出异常。
时间复杂度
常数,即 O(1)
示例
在下面的示例中,arr1 的大小为 0,这就是为什么它将被视为空数组并且成员函数将为 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;
}
让我们编译并运行上面的程序,这将产生以下结果 −
arr1 is empty arr2 is not empty

