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

来自cppreference.com
< cpp‎ | container‎ | deque

 
 
 
 
bool empty() const;
(C++11 前)
bool empty() const noexcept;
(C++11 起)
(C++20 前)
[[nodiscard]] bool empty() const noexcept;
(C++20 起)

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

参数

(无)

返回值

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

复杂度

常数。

示例

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

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

输出:

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

参阅

返回容纳的元素数
(公开成员函数)