Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ranges::all_of,std::ranges::any_of,std::ranges::none_of

      From cppreference.com
      <cpp‎ |algorithm‎ |ranges
       
       
      Algorithm library
      Constrained algorithms and algorithms on ranges(C++20)
      Constrained algorithms, e.g.ranges::copy,ranges::sort, ...
      Execution policies(C++17)
      Sorting and related operations
      Partitioning operations
      Sorting operations
      Binary search operations
      (on partitioned ranges)
      Set operations (on sorted ranges)
      Merge operations (on sorted ranges)
      Heap operations
      Minimum/maximum operations
      (C++11)
      (C++17)
      Lexicographical comparison operations
      Permutation operations
      C library
      Numeric operations
      Operations on uninitialized memory
       
      Constrained algorithms
      All names in this menu belong to namespacestd::ranges
      Non-modifying sequence operations
      Modifying sequence operations
      Partitioning operations
      Sorting operations
      Binary search operations (on sorted ranges)
             
             
      Set operations (on sorted ranges)
      Heap operations
      Minimum/maximum operations
      Permutation operations
      Fold operations
      Operations on uninitialized storage
      Return types
       
      Defined in header<algorithm>
      Call signature
      template<std::input_iterator I,std::sentinel_for<I> S,

               class Proj=std::identity,
               std::indirect_unary_predicate<std::projected<I, Proj>> Pred>

      constexprbool all_of( I first, S last, Pred pred, Proj proj={});
      (1)(since C++20)
      template<ranges::input_range R,class Proj=std::identity,

               std::indirect_unary_predicate<
                    std::projected<ranges::iterator_t<R>,Proj>> Pred>

      constexprbool all_of( R&& r, Pred pred, Proj proj={});
      (2)(since C++20)
      template<std::input_iterator I,std::sentinel_for<I> S,

               class Proj=std::identity,
               std::indirect_unary_predicate<std::projected<I, Proj>> Pred>

      constexprbool any_of( I first, S last, Pred pred, Proj proj={});
      (3)(since C++20)
      template<ranges::input_range R,class Proj=std::identity,

               std::indirect_unary_predicate<
                    std::projected<ranges::iterator_t<R>,Proj>> Pred>

      constexprbool any_of( R&& r, Pred pred, Proj proj={});
      (4)(since C++20)
      template<std::input_iterator I,std::sentinel_for<I> S,

               class Proj=std::identity,
               std::indirect_unary_predicate<std::projected<I, Proj>> Pred>

      constexprbool none_of( I first, S last, Pred pred, Proj proj={});
      (5)(since C++20)
      template<ranges::input_range R,class Proj=std::identity,

               std::indirect_unary_predicate<
                    std::projected<ranges::iterator_t<R>,Proj>> Pred>

      constexprbool none_of( R&& r, Pred pred, Proj proj={});
      (6)(since C++20)
      1) Checks if unary predicatepred returnsfalse for at least one element in the range[firstlast) (after projecting with the projectionproj).
      3) Checks if unary predicatepred returnstrue for at least one element in the range[firstlast) (after projecting with the projectionproj).
      5) Checks if unary predicatepred returnstrue for none of the elements in the range[firstlast) (after projecting with the projectionproj).
      2,4,6) Same as(1,3,5), but usesr as the source range, as if usingranges::begin(r) asfirst andranges::end(r) aslast.

      The function-like entities described on this page arealgorithm function objects (informally known asniebloids), that is:

      Contents

      [edit]Parameters

      first, last - the iterator-sentinel pair defining therange of elements to examine
      r - the range of the elements to examine
      pred - predicate to apply to the projected elements
      proj - projection to apply to the elements

      [edit]Return value

      1,2)true ifstd::invoke(pred,std::invoke(proj,*i))!=false for every iteratori in the range,false otherwise. Returnstrue if the range is empty.
      3,4)true ifstd::invoke(pred,std::invoke(proj,*i))!=false for at least one iteratori in the range,false otherwise. Returnsfalse if the range is empty.
      5,6)true ifstd::invoke(pred,std::invoke(proj,*i))==false for every iteratori in the range,false otherwise. Returnstrue if the range is empty.
      Range has sometrue elementYesNo
       Range has somefalse element YesNoYes    No[1]
      all_offalsetruefalsetrue
      any_oftruetrue  false    false  
      none_of  false    false  truetrue
      1. The range is empty in this case.

      [edit]Complexity

      At mostlast- first applications of the predicate and the projection.

      [edit]Possible implementation

      all_of (1,2)
      struct all_of_fn{template<std::input_iterator I,std::sentinel_for<I> S,class Proj=std::identity,std::indirect_unary_predicate<std::projected<I, Proj>> Pred>constexprbool operator()(I first, S last, Pred pred, Proj proj={})const{returnranges::find_if_not(first, last,std::ref(pred),std::ref(proj))== last;} template<ranges::input_range R,class Proj=std::identity,std::indirect_unary_predicate<                 std::projected<ranges::iterator_t<R>,Proj>> Pred>constexprbool operator()(R&& r, Pred pred, Proj proj={})const{return operator()(ranges::begin(r),ranges::end(r),std::ref(pred),std::ref(proj));}}; inlineconstexpr all_of_fn all_of;
      any_of (3,4)
      struct any_of_fn{template<std::input_iterator I,std::sentinel_for<I> S,class Proj=std::identity,std::indirect_unary_predicate<std::projected<I, Proj>> Pred>constexprbool operator()(I first, S last, Pred pred, Proj proj={})const{returnranges::find_if(first, last,std::ref(pred),std::ref(proj))!= last;} template<ranges::input_range R,class Proj=std::identity,std::indirect_unary_predicate<                 std::projected<ranges::iterator_t<R>,Proj>> Pred>constexprbool operator()(R&& r, Pred pred, Proj proj={})const{return operator()(ranges::begin(r),ranges::end(r),std::ref(pred),std::ref(proj));}}; inlineconstexpr any_of_fn any_of;
      none_of (5,6)
      struct none_of_fn{template<std::input_iterator I,std::sentinel_for<I> S,class Proj=std::identity,std::indirect_unary_predicate<std::projected<I, Proj>> Pred>constexprbool operator()(I first, S last, Pred pred, Proj proj={})const{returnranges::find_if(first, last,std::ref(pred),std::ref(proj))== last;} template<ranges::input_range R,class Proj=std::identity,std::indirect_unary_predicate<                 std::projected<ranges::iterator_t<R>,Proj>> Pred>constexprbool operator()(R&& r, Pred pred, Proj proj={})const{return operator()(ranges::begin(r),ranges::end(r),std::ref(pred),std::ref(proj));}}; inlineconstexpr none_of_fn none_of;

      [edit]Example

      Run this code
      #include <algorithm>#include <functional>#include <iostream>#include <iterator>#include <numeric>#include <vector> namespace ranges= std::ranges; constexprbool some_of(auto&& r,auto&& pred)// some but not all{return not(ranges::all_of(r, pred) orranges::none_of(r, pred));} constexprauto w={1,2,3};static_assert(!some_of(w,[](int x){return x<1;}));static_assert( some_of(w,[](int x){return x<2;}));static_assert(!some_of(w,[](int x){return x<4;})); int main(){std::vector<int> v(10,2);std::partial_sum(v.cbegin(), v.cend(), v.begin());std::cout<<"Among the numbers: ";ranges::copy(v,std::ostream_iterator<int>(std::cout," "));std::cout<<'\n'; if(ranges::all_of(v.cbegin(), v.cend(),[](int i){return i%2==0;}))std::cout<<"All numbers are even\n"; if(ranges::none_of(v,std::bind(std::modulus<int>(), std::placeholders::_1,2)))std::cout<<"None of them are odd\n"; auto DivisibleBy=[](int d){return[d](int m){return m% d==0;};}; if(ranges::any_of(v, DivisibleBy(7)))std::cout<<"At least one number is divisible by 7\n";}

      Output:

      Among the numbers: 2 4 6 8 10 12 14 16 18 20All numbers are evenNone of them are oddAt least one number is divisible by 7

      [edit]See also

      (C++11)(C++11)(C++11)
      checks if a predicate istrue for all, any or none of the elements in a range
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/algorithm/ranges/all_any_none_of&oldid=180449"

      [8]ページ先頭

      ©2009-2025 Movatter.jp