命名空间
变体
操作

std::array<T,N>::empty

来自 cppreference.cn
< cpp‎ | container‎ | array
 
 
 
 
constexpr bool empty() const noexcept;
(since C++11)

检查容器是否为空,即是否 begin() == end()

目录

[edit] 返回值

true 如果容器为空,则为 false 否则为假。

[edit] 复杂度

常数。

[edit] 示例

以下代码使用 empty 来检查 std::array 是否包含任何元素

#include <array>
#include <iostream>
 
int main()
{
    std::array<int, 4> numbers{3, 1, 4, 1};
    std::array<int, 0> no_numbers;
 
    std::cout << std::boolalpha;
    std::cout << "numbers.empty(): " << numbers.empty() << '\n';
    std::cout << "no_numbers.empty(): " << no_numbers.empty() << '\n';
}

输出

numbers.empty(): false
no_numbers.empty(): true

[edit] 参见

返回元素数量
(公共成员函数) [edit]
(C++17)
检查容器是否为空
(函数模板) [edit]