bool empty() const; // (1) C++03
bool empty() const noexcept; // (1) C++11
[[nodiscard]] constexpr bool empty() const noexcept; // (1) C++20
constexpr bool empty() const noexcept; // (1) C++26
概要
コンテナが空かどうかを判定する
戻り値
コンテナが空であればtrue、そうでなければfalseを返す。
計算量
定数時間
例外
投げない
計算量
定数時間
備考
a.empty() と a.begin() == a.end() は同じ結果になる
。
例
#include <iostream>
#include <vector>
int main()
{
// 空
{
std::vector<int> v;
bool b = v.empty();
std::cout << std::boolalpha << b << std::endl;
}
// 空じゃない
{
std::vector<int> v = {1, 2, 3};
bool b = v.empty();
std::cout << std::boolalpha << b << std::endl;
}
}
出力
true
false
参照
- P0600R1
[[nodiscard]]in the Library- C++20で
[[nodiscard]]が付加された
- C++20で
- P1004R2 Making
std::vectorconstexpr- C++20で
constexprが付加された
- C++20で
- P2422R1 Remove
nodiscardannotations from the standard library specification- C++26で
[[nodiscard]]指定が削除された
- C++26で