Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ranges::empty

      From cppreference.com
      <cpp‎ |ranges
       
       
      Ranges library
      Range adaptors
       
      Defined in header<ranges>
      Defined in header<iterator>
      inlinenamespace/*unspecified*/{

         inlineconstexprauto empty=/*unspecified*/;

      }
      (since C++20)
      (customization point object)
      Call signature
      template<class T>

          requires/* see below */

      constexprbool empty( T&& t);
      (since C++20)

      Determines whether or nott has any elements.

      A call toranges::empty isexpression-equivalent to:

      1. bool(t.empty()), if that expression is valid.
      2. Otherwise,(ranges::size(t)==0), if that expression is valid.
      3. Otherwise,bool(ranges::begin(t)==ranges::end(t)), if that expression is valid anddecltype(ranges::begin(t)) modelsstd::forward_iterator.

      In all other cases, a call toranges::empty is ill-formed, which can result insubstitution failure whenranges::empty(t) appears in the immediate context of a template instantiation.

      Customization point objects

      The nameranges::empty denotes acustomization point object, which is a constfunction object of aliteralsemiregular class type. SeeCustomizationPointObject for details.

      [edit]Example

      Run this code
      #include <iostream>#include <ranges>#include <vector> template<std::ranges::input_range R>void print(char id, R&& r){if(std::ranges::empty(r)){std::cout<<'\t'<< id<<") Empty\n";return;} std::cout<<'\t'<< id<<") Elements:";for(constauto& element: r)std::cout<<' '<< element;std::cout<<'\n';} int main(){{auto v=std::vector<int>{1,2,3};std::cout<<"(1) ranges::empty uses std::vector::empty:\n";        print('a', v);         v.clear();        print('b', v);}{std::cout<<"(2) ranges::empty uses ranges::size(initializer_list):\n";auto il={7,8,9};        print('a', il);         print('b',std::initializer_list<int>{});}{std::cout<<"(2) ranges::empty on a raw array uses ranges::size:\n";int array[]={4,5,6};// array has a known bound        print('a', array);}{struct Scanty:privatestd::vector<int>{usingstd::vector<int>::begin;usingstd::vector<int>::end;usingstd::vector<int>::push_back;// Note: both empty() and size() are hidden}; std::cout<<"(3) calling ranges::empty on an object w/o empty() or size():\n";        Scanty y;        print('a', y);        y.push_back(42);        print('b', y);}}

      Output:

      (1) ranges::empty uses std::vector::empty:        a) Elements: 1 2 3        b) Empty(2) ranges::empty uses ranges::size(initializer_list):        a) Elements: 7 8 9        b) Empty(2) ranges::empty on a raw array uses ranges::size:        a) Elements: 4 5 6(3) calling ranges::empty on an object w/o empty() or size():        a) Empty        b) Elements: 42

      [edit]See also

      (C++17)
      checks whether the container is empty
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/ranges/empty&oldid=160900"

      [8]ページ先頭

      ©2009-2025 Movatter.jp