std::vector<T,Allocator>::empty
来自 zh.cppreference.net
bool empty() const;
|
(自 C++11 起为 noexcept) (自 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
另请参阅
|
返回元素数量
(公开成员函数) |
|
|
(C++17)
|
检查容器是否为空
(函数模板) |