std:: empty
From cppreference.net
|
헤더에 정의됨
<array>
|
||
|
헤더에 정의됨
<deque>
|
||
|
헤더에 정의됨
<flat_map>
|
||
|
헤더에 정의됨
<flat_set>
|
||
|
헤더에 정의됨
<forward_list>
|
||
|
헤더에 정의됨
<inplace_vector>
|
||
|
헤더에 정의됨
<iterator>
|
||
|
헤더에 정의됨
<list>
|
||
|
헤더에 정의됨
<map>
|
||
|
헤더에 정의됨
<regex>
|
||
|
헤더에 정의됨
<set>
|
||
|
헤더에 정의됨
<span>
|
||
|
헤더에 정의됨
<string>
|
||
|
헤더에 정의됨
<string_view>
|
||
|
헤더에 정의됨
<unordered_map>
|
||
|
헤더에 정의됨
<unordered_set>
|
||
|
헤더에 정의됨
<vector>
|
||
|
template
<
class
C
>
constexpr auto empty ( const C & c ) - > decltype ( c. empty ( ) ) ; |
(1) | (C++17부터) |
|
template
<
class
T,
std::
size_t
N
>
constexpr bool empty ( const T ( & array ) [ N ] ) noexcept ; |
(2) | (C++17부터) |
|
template
<
class
E
>
constexpr bool empty ( std:: initializer_list < E > il ) noexcept ; |
(3) | (C++17부터) |
주어진 범위가 비어 있는지 여부를 반환합니다.
1)
반환 값
c.
empty
(
)
.
2)
반환값
false
.
3)
반환값
il.
size
(
)
==
0
.
목차 |
매개변수
| c | - |
empty
멤버 함수를 가진 컨테이너 또는 뷰
|
| array | - | 임의 타입의 배열 |
| il | - | std::initializer_list |
반환값
1)
c.
empty
(
)
2)
false
3)
il.
size
(
)
==
0
예외
1)
구현에서 정의된 예외를 throw할 수 있습니다.
참고 사항
std::initializer_list
에 대한 오버로드는
empty
멤버 함수를 가지고 있지 않기 때문에 필요합니다.
| 기능 테스트 매크로 | 값 | 표준 | 기능 |
|---|---|---|---|
__cpp_lib_nonmember_container_access
|
201411L
|
(C++17) |
std::size()
,
std::data()
, 및
std::empty()
|
가능한 구현
| 첫 번째 버전 |
|---|
template<class C> [[nodiscard]] constexpr auto empty(const C& c) -> decltype(c.empty()) { return c.empty(); } |
| 두 번째 버전 |
template<class T, std::size_t N> [[nodiscard]] constexpr bool empty(const T (&array)[N]) noexcept { return false; } |
| 세 번째 버전 |
template<class E> [[nodiscard]] constexpr bool empty(std::initializer_list<E> il) noexcept { return il.size() == 0; } |
예제
이 코드 실행
#include <iostream> #include <vector> template<class T> void print(const T& container) { if (std::empty(container)) std::cout << "Empty\n"; else { std::cout << "Elements:"; for (const auto& element : container) std::cout << ' ' << element; std::cout << '\n'; } } int main() { std::vector<int> c = {1, 2, 3}; print(c); c.clear(); print(c); int array[] = {4, 5, 6}; print(array); auto il = {7, 8, 9}; print(il); }
출력:
Elements: 1 2 3 Empty Elements: 4 5 6 Elements: 7 8 9
참고 항목
|
(C++20)
|
범위가 비어 있는지 여부를 확인합니다
(커스터마이제이션 포인트 객체) |