Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/iterator/empty"

From cppreference.com
< cpp‎ | iterator
m (s/method/member function)
(Notes, See also)
Line 39: Line 39:
 
{{dcl end}}
 
{{dcl end}}
  
Returns whether the given container is empty.
+
Returns whether the given range is empty.
  
 
@1@ returns {{c|c.empty()}}
 
@1@ returns {{c|c.empty()}}
Line 47: Line 47:
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | c | a container with an {{tt|empty}} member function}}
+
{{par | c | a container or view with an {{tt|empty}} member function}}
 
{{par | array | an array of arbitrary type}}
 
{{par | array | an array of arbitrary type}}
 
{{par | il | an initializer list}}
 
{{par | il | an initializer list}}
Line 53: Line 53:
  
 
===Return value===
 
===Return value===
{{c|true}} if the container doesn't have any element.
+
{{c|true}} if the range doesn't have any element.
 +
 
 +
{{cpp/impldef exception}}
 +
 
 +
===Notes===
 +
The overload for {{lc|std::initializer_list}} is necessary because it does not have a member function {{tt|empty}}.
  
 
===Possible implementation===
 
===Possible implementation===
Line 73: Line 78:
 
constexpr bool empty(std::initializer_list<E> il) noexcept
 
constexpr bool empty(std::initializer_list<E> il) noexcept
 
{
 
{
     return il.size() {{==}} 0;
+
     return il.size() == 0;
 
}
 
}
 
}}
 
}}
Line 120: Line 125:
 
}}
 
}}
  
{{langlinks|es|ja|zh}}
+
===See also===
 +
{{dsc begin}}
 +
{{dsc inc | cpp/ranges/dsc empty}}
 +
{{dsc end}}
 +
 
 +
{{langlinks|es|ja|ru|zh}}

Revision as of 23:39, 3 April 2021

 
 
Iterator library
Iterator concepts
Iterator primitives
Algorithm concepts and utilities
Indirect callable concepts
Common algorithm requirements
(C++20)
(C++20)
(C++20)
Utilities
(C++20)
Iterator adaptors
Range access
(C++11)(C++14)
(C++14)(C++14)  
(C++11)(C++14)
(C++14)(C++14)  
(C++17)(C++20)
empty
(C++17)
(C++17)
 
Defined in header <array>
Defined in header <deque>
Defined in header <forward_list>
Defined in header <iterator>
Defined in header <list>
Defined in header <map>
Defined in header <regex>
Defined in header <set>
Defined in header <span>
(since C++20)
Defined in header <string>
Defined in header <string_view>
Defined in header <unordered_map>
Defined in header <unordered_set>
Defined in header <vector>
(1)
template <class C>
constexpr auto empty(const C& c) -> decltype(c.empty());
(since C++17)
(until C++20)
template <class C>
[[nodiscard]] constexpr auto empty(const C& c) -> decltype(c.empty());
(since C++20)
(2)
template <class T, std::size_t N>
constexpr bool empty(const T (&array)[N]) noexcept;
(since C++17)
(until C++20)
template <class T, std::size_t N>
[[nodiscard]] constexpr bool empty(const T (&array)[N]) noexcept;
(since C++20)
(3)
template <class E>
constexpr bool empty(std::initializer_list<E> il) noexcept;
(since C++17)
(until C++20)
template <class E>
[[nodiscard]] constexpr bool empty(std::initializer_list<E> il) noexcept;
(since C++20)

Returns whether the given range is empty.

1) returns c.empty()
2) returns false
3) returns il.size() == 0

Contents

Parameters

c - a container or view with an empty member function
array - an array of arbitrary type
il - an initializer list

Return value

true if the range doesn't have any element.

Exceptions

May throw implementation-defined exceptions.

Notes

The overload for std::initializer_list is necessary because it does not have a member function empty.

Possible implementation

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;
}

Example

#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);
}

Output:

Elements: 1 2 3
Empty
Elements: 4 5 6
Elements: 7 8 9

See also

checks whether a range is empty
(customization point object)[edit]