◐ Shell
clean mode source ↗

std::vector<T,Allocator>::empty - cppreference.com

提供: cppreference.com

<tbody> </tbody> <tbody class="t-dcl-rev "> </tbody><tbody> </tbody>

bool empty() const;

(C++11未満)

bool empty() const noexcept;

(C++11以上)
(C++20未満)

[[nodiscard]] bool empty() const noexcept;

(C++20以上)

コンテナの持っている要素が無い、つまり begin() == end() かどうかを調べます。

引数

(なし)

戻り値

コンテナが空であれば true、そうでなければ false

計算量

一定。

#include <vector>
#include <iostream>
 
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

関連項目