array::empty
描述 (Description)
C ++函数std::array::empty()测试数组的大小是否为零。
声明 (Declaration)
以下是std :: array :: empty()函数形式std :: array标头的声明。
constexpr bool empty() noexcept;
参数 (Parameters)
没有
返回值 (Return Value)
如果数组大小为0,则返回true,否则返回false。
异常 (Exceptions)
该成员函数从不抛出异常。
时间复杂
常数即O(1)
例子 (Example)
在下面的示例中,arr1的大小为0,这就是为什么它将被视为空数组,而成员函数将返回arr1的true值。
#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