std::map<Key,T,Compare,Allocator>::empty

從 cppreference.com
< cpp‎ | container‎ | map
 
 
 
 
bool empty() const;
(C++11 起為 noexcept)
(C++26 起為 constexpr)

檢查容器是否無元素。

目錄

[編輯] 返回值

容器為空時返回 true,否則返回 false

[編輯] 複雜度

常數。

[編輯] 示例

下列代碼用 empty 檢查 std::map<int, int> 是否含有任何元素:

#include <iostream>
#include <map>
#include <utility>
 
int main()
{
    std::map<int,int> numbers;
    std::cout << std::boolalpha;
    std::cout << "起初, numbers.empty(): " << numbers.empty() << '\n';
 
    numbers.emplace(42, 13);
    numbers.insert(std::make_pair(13317, 123));
    std::cout << "添加元素后, numbers.empty(): " << numbers.empty() << '\n';
}

輸出:

起初, numbers.empty(): true
添加元素后, numbers.empty(): false

[編輯] 參閱

返回元素數
(公開成員函數) [編輯]
(C++17)
檢查容器是否為空
(函數模板) [編輯]