Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::empty

      From cppreference.com
      <cpp‎ |iterator
       
       
      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>
      constexprauto empty(const C& c)-> decltype(c.empty());
      (1)(since C++17)
      template<class T,std::size_t N>
      constexprbool empty(const T(&array)[N])noexcept;
      (2)(since C++17)
      template<class E>
      constexprbool empty(std::initializer_list<E> il)noexcept;
      (3)(since C++17)

      Returns whether the given range is empty.

      1) Returnsc.empty().
      2) Returnsfalse.
      3) Returnsil.size()==0.

      Contents

      [edit]Parameters

      c - a container or view with anempty member function
      array - an array of arbitrary type
      il - anstd::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 forstd::initializer_list is necessary because it does not have a member functionempty.

      Feature-test macroValueStdFeature
      __cpp_lib_nonmember_container_access201411L(C++17)std::size(),std::data(), andstd::empty()

      [edit]Possible implementation

      First version
      template<class C>[[nodiscard]]constexprauto empty(const C& c)-> decltype(c.empty()){return c.empty();}
      Second version
      template<class T,std::size_t N>[[nodiscard]]constexprbool empty(const T(&array)[N])noexcept{returnfalse;}
      Third version
      template<class E>[[nodiscard]]constexprbool empty(std::initializer_list<E> il)noexcept{return il.size()==0;}

      [edit]Example

      Run this code
      #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(constauto& 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 3EmptyElements: 4 5 6Elements: 7 8 9

      [edit]See also

      checks whether a range is empty
      (customization point object)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/iterator/empty&oldid=173011"

      [8]ページ先頭

      ©2009-2025 Movatter.jp