A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://en.cppreference.com/w/cpp/language/../algorithm/../ranges/../iterator/empty.html below:

std::empty - cppreference.com

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) (3) (since C++17)

Returns whether the given range is empty.

1) Returns c.empty().

2) Returns false.

3) Returns il.size() == 0.

[edit] Parameters c - a container or view with an empty member function array - an array of arbitrary type il - an std::initializer_list [edit] Return value

1) c.empty()

2) false

3) il.size() == 0

[edit] Exceptions

1) May throw implementation-defined exceptions.

[edit] Notes

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

[edit] Possible implementation First version
template<class C>
[[nodiscard]] constexpr auto empty(const C& c) -> decltype(c.empty())
{
    return c.empty();
}
Second version
template<class T, std::size_t N>
[[nodiscard]] constexpr bool empty(const T (&array)[N]) noexcept
{
    return false;
}
Third version
template<class E>
[[nodiscard]] constexpr bool empty(std::initializer_list<E> il) noexcept
{
    return il.size() == 0;
}
[edit] 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
[edit] See also

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4