Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/iterator/empty"

From cppreference.com
< cpp‎ | iterator
m (Possible implementation: +links to synopsis)
(- nodiscard)
 
(4 intermediate revisions by 3 users not shown)
Line 2: Line 2:
 
{{cpp/iterator/navbar}}
 
{{cpp/iterator/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl header|array}}
+
{{cpp/iterator/range access headers}}
{{dcl header|deque}}
+
{{dcla|num=1|since=c++17|
{{dcl header|forward_list}}
+
{{dcl header|iterator}}
+
{{dcl header|list}}
+
{{dcl header|map}}
+
{{dcl header|regex}}
+
{{dcl header|set}}
+
{{dcl header|span|notes={{mark since c++20}}}}
+
{{dcl header|string}}
+
{{dcl header|string_view}}
+
{{dcl header|unordered_map}}
+
{{dcl header|unordered_set}}
+
{{dcl header|vector}}
+
{{dcl end}}
+
{{dcl begin}}
+
{{dcl rev multi|anchor=1|num=1|since1=c++17|dcl1=
+
 
template< class C >
 
template< class C >
 
constexpr auto empty( const C& c ) -> decltype(c.empty());
 
constexpr auto empty( const C& c ) -> decltype(c.empty());
|since2=c++20|dcl2=
 
template< class C >
 
[[nodiscard]] constexpr auto empty( const C& c ) -> decltype(c.empty());
 
 
}}
 
}}
{{dcl rev multi|anchor=2|num=2|since1=c++17|dcl1=
+
{{dcla|num=2|since=c++17|
 
template< class T, std::size_t N >
 
template< class T, std::size_t N >
 
constexpr bool empty( const T (&array)[N] ) noexcept;
 
constexpr bool empty( const T (&array)[N] ) noexcept;
|since2=c++20|dcl2=
 
template< class T, std::size_t N >
 
[[nodiscard]] constexpr bool empty( const T (&array)[N] ) noexcept;
 
 
}}
 
}}
{{dcl rev multi|anchor=3|num=3|since1=c++17|dcl1=
+
{{dcla|num=3|since=c++17|
 
template< class E >
 
template< class E >
 
constexpr bool empty( std::initializer_list<E> il ) noexcept;
 
constexpr bool empty( std::initializer_list<E> il ) noexcept;
|since2=c++20|dcl2=
 
template< class E >
 
[[nodiscard]] constexpr bool empty( std::initializer_list<E> il ) noexcept;
 
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
Line 43: Line 19:
 
Returns whether the given range is empty.
 
Returns whether the given range is empty.
  
@1@ returns {{c|c.empty()}}
+
@1@ Returns {{c|c.empty()}}.
@2@ returns {{c|false}}
+
@2@ Returns {{c|false}}.
@3@ returns {{c|il.size() {{==}} 0}}
+
@3@ Returns {{c|1=il.size() == 0}}.
  
 
===Parameters===
 
===Parameters===
Line 51: Line 27:
 
{{par|c|a container or view 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 {{lc|std::initializer_list}}}}
 
{{par end}}
 
{{par end}}
  
 
===Return value===
 
===Return value===
{{c|true}} if the range doesn't have any element.
+
@1@ {{c|c.empty()}}
 +
@2@ {{c|false}}
 +
@3@ {{c|1=il.size() == 0}}
  
 
===Exceptions===
 
===Exceptions===
Line 68: Line 46:
 
{{eq impl
 
{{eq impl
 
|ver1=1|1=
 
|ver1=1|1=
template <class C>
+
template<class C>
 
[[nodiscard]] constexpr auto empty(const C& c) -> decltype(c.empty())
 
[[nodiscard]] constexpr auto empty(const C& c) -> decltype(c.empty())
 
{
 
{
Line 74: Line 52:
 
}
 
}
 
|ver2=2|2=
 
|ver2=2|2=
template <class T, std::size_t N>
+
template<class T, std::size_t N>
 
[[nodiscard]] constexpr bool empty(const T (&array)[N]) noexcept
 
[[nodiscard]] constexpr bool empty(const T (&array)[N]) noexcept
 
{
 
{
Line 80: Line 58:
 
}
 
}
 
|ver3=3|3=
 
|ver3=3|3=
template <class E>
+
template<class E>
 
[[nodiscard]] constexpr bool empty(std::initializer_list<E> il) noexcept
 
[[nodiscard]] constexpr bool empty(std::initializer_list<E> il) noexcept
 
{
 
{
Line 89: Line 67:
 
===Example===
 
===Example===
 
{{example
 
{{example
|
 
 
|code=
 
|code=
 
#include <iostream>
 
#include <iostream>
 
#include <vector>
 
#include <vector>
  
template <class T>
+
template<class T>
 
void print(const T& container)
 
void print(const T& container)
 
{
 
{
     if ( std::empty(container) )
+
     if (std::empty(container))
    {
+
 
         std::cout << "Empty\n";
 
         std::cout << "Empty\n";
    }
 
 
     else
 
     else
 
     {
 
     {
 
         std::cout << "Elements:";
 
         std::cout << "Elements:";
         for ( const auto& element : container )
+
         for (const auto& element : container)
 
             std::cout << ' ' << element;
 
             std::cout << ' ' << element;
 
         std::cout << '\n';
 
         std::cout << '\n';
Line 112: Line 87:
 
int main()
 
int main()
 
{
 
{
     std::vector<int> c = { 1, 2, 3 };
+
     std::vector<int> c = {1, 2, 3};
 
     print(c);
 
     print(c);
 
     c.clear();
 
     c.clear();
 
     print(c);
 
     print(c);
+
   
     int array[] = { 4, 5, 6 };
+
     int array[] = {4, 5, 6};
 
     print(array);
 
     print(array);
+
   
     auto il = { 7, 8, 9 };
+
     auto il = {7, 8, 9};
 
     print(il);
 
     print(il);
 
}
 
}

Latest revision as of 01:57, 1 July 2024

 
 
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 <flat_map>
Defined in header <flat_set>
Defined in header <forward_list>
Defined in header <inplace_vector>
Defined in header <iterator>
Defined in header <list>
Defined in header <map>
Defined in header <regex>
Defined in header <set>
Defined in header <span>
Defined in header <string>
Defined in header <string_view>
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 range is empty.

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

Contents

[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.

Feature-test macro Value Std Feature
__cpp_lib_nonmember_container_access 201411L (C++17) std::size(), std::data(), and std::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

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