命名空间
变体
操作

std::vector<T,Allocator>::empty

来自 cppreference.cn
< cpp‎ | 容器‎ | vector
 
 
 
 
bool empty() const;
(C++11 起无异常抛出)
(C++20 起为 constexpr)

检查容器是否没有元素,即 begin() == end() 是否为真。

目录

[编辑] 返回值

如果容器为空,则为 true;否则为 false

[编辑] 复杂度

常数时间。

[编辑] 示例

#include <iostream>
#include <vector>
 
int main()
{
    std::cout << std::boolalpha;
 
    std::vector<int> numbers;
    std::cout << "Initially, numbers.empty(): " << numbers.empty() << '\n';
 
    numbers.push_back(42);
    std::cout << "After adding elements, numbers.empty(): " << numbers.empty() << '\n';
}

输出

Initially, numbers.empty(): true
After adding elements, numbers.empty(): false

[编辑] 参阅

返回元素数量
(public member function) [编辑]
(C++17)
检查容器是否为空
(function template) [编辑]