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

来自cppreference.com

 
 
容器库
序列
(C++11)
关联
无序关联
适配器
视图
(C++20)
 
 
bool empty() const noexcept;
(C++11 起)
(C++20 前)
[[nodiscard]] bool empty() const noexcept;
(C++20 起)

检查容器是否无元素,即是否 begin() == end()

参数

(无)

返回值

若容器为空则为 true ,否则为 false

复杂度

常数。

示例

下列代码用 empty 检查 std::forward_list<int> 是否含任何元素:

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

输出:

Initially, numbers.empty(): 1
After adding elements, numbers.empty(): 0