名稱空間
變體
操作

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

來自 cppreference.com
< 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) [編輯]