std::empty

std::empty (범위가 비어 있는지 확인)

주어진 범위가 비어 있는지 돌려주는 함수예요. C++17부터 있어요.

출처: cppreference

본문

다양한 컨테이너 헤더와 <iterator> 헤더에 정의돼 있어요.

// (1) C++17
template< class C >
constexpr auto empty( const C& c ) noexcept(noexcept(c.empty()))
    -> decltype(c.empty());

// (2) C++17 (noexcept)
template< class T, std::size_t N >
constexpr bool empty( const T (&array)[N] ) noexcept;

주어진 범위가 비어 있는지 돌려줘요.

    1. c.empty()를 반환해요.
    1. false를 반환해요.

매개변수

  • cempty 멤버 함수가 있는 컨테이너·뷰
  • array — 임의 타입의 배열

반환값

범위에 요소가 없으면 true, 있으면 false예요(배열은 항상 false).

예제

#include <iostream>
#include <vector>
#include <iterator>

int main()
{
    std::vector<int> v;
    std::cout << std::boolalpha << std::empty(v) << '\n'; // "true"
    v.push_back(1);
    std::cout << std::empty(v) << '\n'; // "false"
}

더 알아보기 (Learn more)

cppreference