Defined in header
<iterator> | ||
---|---|---|
Defined in header
<array> | ||
Defined in header
<deque> | ||
Defined in header
<forward_list> | ||
Defined in header
<list> | ||
Defined in header
<map> | ||
Defined in header
<regex> | ||
Defined in header
<set> | ||
Defined in header
<string> | ||
Defined in header
<unordered_map> | ||
Defined in header
<unordered_set> | ||
Defined in header
<vector> | ||
template <class C> constexpr auto empty(const C& c) -> decltype(c.empty()); | (1) | (since C++17) |
template <class T, std::size_t N> constexpr bool empty(const T (&array)[N]) noexcept; | (2) | (since C++17) |
template <class E> constexpr bool empty(std::initializer_list<E> il) noexcept; | (3) | (since C++17) |
Returns whether the given container is empty.
c.empty()
false
il.size() == 0
c | - | a container with an empty method |
array | - | an array of arbitrary type |
il | - | an initializer list |
true
if the container doesn't have any element.
noexcept
specification: noexcept
First version |
---|
template <class C> constexpr auto empty(const C& c) -> decltype(c.empty()) { return c.empty(); } |
Second version |
template <class T, std::size_t N> constexpr bool empty(const T (&array)[N]) noexcept { return false; } |
Third version |
template <class E> 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 << "Elements:\n"; for ( const auto& element : container ) std::cout << element << '\n'; } else { std::cout << "Empty\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); }
Output:
Elements: 1 2 3 Empty Elements: 4 5 6 Elements: 7 8 9
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/iterator/empty