Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ranges::contains,std::ranges::contains_subrange

      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
      (1)
      template<std::input_iterator I,std::sentinel_for<I> S,

               class T,
               class Proj=std::identity>
          requiresstd::indirect_binary_predicate
                       <ranges::equal_to, std::projected<I, Proj>,const T*>

      constexprbool contains( I first, S last,const T& value, Proj proj={});
      (since C++23)
      (until C++26)
      template<std::input_iterator I,std::sentinel_for<I> S,

               class Proj=std::identity,
               class T= std::projected_value_t<I, Proj>>
          requiresstd::indirect_binary_predicate
                       <ranges::equal_to, std::projected<I, Proj>,const T*>

      constexprbool contains( I first, S last,const T& value, Proj proj={});
      (since C++26)
      (2)
      template<ranges::input_range R,

               class T,
               class Proj=std::identity>
          requiresstd::indirect_binary_predicate
                       <ranges::equal_to,
                        std::projected<ranges::iterator_t<R>, Proj>,const T*>

      constexprbool contains( R&& r,const T& value, Proj proj={});
      (since C++23)
      (until C++26)
      template<ranges::input_range R,

               class Proj=std::identity,
               class T= std::projected_value_t<ranges::iterator_t<R>, Proj>>
          requiresstd::indirect_binary_predicate
                       <ranges::equal_to,
                        std::projected<ranges::iterator_t<R>, Proj>,const T*>

      constexprbool contains( R&& r,const T& value, Proj proj={});
      (since C++26)
      template<std::forward_iterator I1,std::sentinel_for<I1> S1,

               std::forward_iterator I2,std::sentinel_for<I2> S2,
               class Pred=ranges::equal_to,
               class Proj1=std::identity,class Proj2=std::identity>
      requiresstd::indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
      constexprbool contains_subrange( I1 first1, S1 last1, I2 first2, S2 last2,
                                        Pred pred={},

                                        Proj1 proj1={}, Proj2 proj2={});
      (3)(since C++23)
      template<ranges::forward_range R1,ranges::forward_range R2,

               class Pred=ranges::equal_to,
               class Proj1=std::identity,class Proj2=std::identity>
          requiresstd::indirectly_comparable
                       <ranges::iterator_t<R1>,ranges::iterator_t<R2>,
                        Pred, Proj1, Proj2>
      constexprbool contains_subrange( R1&& r1, R2&& r2, Pred pred={},

                                        Proj1 proj1={}, Proj2 proj2={});
      (4)(since C++23)
      1,2) Checks whether or not a given range contains the valuevalue.
      1) The source range is[firstlast).
      2) The source range is[ranges::begin(r)ranges::end(r)).
      3) Checks whether or not a given range is a subrange of another range.
      3) The first source range is[first1last1), and the second source range is[first2last2).
      4) The first source range is[ranges::begin(r1)ranges::end(r1)), and the second source range is[ranges::begin(r2)ranges::end(r2)).

      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
      value - value to compare the elements to
      pred - predicate to apply to the projected elements
      proj - projection to apply to the elements

      [edit]Return value

      1)ranges::find(std::move(first), last, value, proj)!= last
      2)ranges::find(std::move(ranges::begin(r)),ranges::end(r), value, proj)!=ranges::end(r)
      3)first2== last2||!ranges::search(first1, last1, first2, last2, pred, proj1, proj2).empty()
      4)ranges::begin(r2)==ranges::end(r2)||
         !ranges::search(ranges::begin(r1),ranges::end(r1),
                         ranges::begin(r2),ranges::end(r2), pred, proj1, proj2).empty()

      [edit]Complexity

      1) At mostranges::distance(first, last) comparisons.
      2) At mostranges::distance(r) comparisons.
      3) At mostranges::distance(first1, last1)*ranges::distance(first2, last2) comparisons.
      4) At mostranges::distance(r1)*ranges::distance(r2) comparisons.

      [edit]Notes

      In C++20, one may implement acontains function withranges::find(haystack, needle)!=ranges::end(haystack) orcontains_subrange with!ranges::search(haystack, needle).empty().

      ranges::contains_subrange, likeranges::search, and unlikestd::search, has no support forsearchers (such asstd::boyer_moore_searcher).

      Feature-test macroValueStdFeature
      __cpp_lib_ranges_contains202207L(C++23)ranges::contains andranges::contains_subrange
      __cpp_lib_algorithm_default_value_type202403L(C++26)List-initialization for algorithms(1,2)

      [edit]Possible implementation

      contains (1,2)
      struct __contains_fn{template<std::input_iterator I,std::sentinel_for<I> S,class Proj=std::identity,class T= std::projected_value_t<I, Proj>>    requiresstd::indirect_binary_predicate<ranges::equal_to, std::projected<I, Proj>,const T*>constexprbool operator()(I first, S last,const T& value, Proj proj={})const{returnranges::find(std::move(first), last, value, proj)!= last;} template<ranges::input_range R,class Proj=std::identity,class T= std::projected_value_t<ranges::iterator_t<R>, Proj>>    requiresstd::indirect_binary_predicate<ranges::equal_to,                                            std::projected<ranges::iterator_t<R>, Proj>,const T*>constexprbool operator()(R&& r,const T& value, Proj proj={})const{returnranges::find(std::move(ranges::begin(r)),ranges::end(r), value, proj)!=ranges::end(r);}}; inlineconstexpr __contains_fn contains{};
      contains_subrange (3,4)
      struct __contains_subrange_fn{template<std::forward_iterator I1,std::sentinel_for<I1> S1,std::forward_iterator I2,std::sentinel_for<I2> S2,class Pred=ranges::equal_to,class Proj1=std::identity,class Proj2=std::identity>    requiresstd::indirectly_comparable<I1, I2, Pred, Proj1, Proj2>constexprbool operator()(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred={},                              Proj1 proj1={}, Proj2 proj2={})const{return(first2== last2)||!ranges::search(first1, last1, first2, last2,                                   pred, proj1, proj2).empty();} template<ranges::forward_range R1,ranges::forward_range R2,class Pred=ranges::equal_to,class Proj1=std::identity,class Proj2=std::identity>    requiresstd::indirectly_comparable<ranges::iterator_t<R1>,ranges::iterator_t<R2>, Pred, Proj1, Proj2>constexprbool operator()(R1&& r1, R2&& r2, Pred pred={},                              Proj1 proj1={}, Proj2 proj2={})const{return(first2== last2)||!ranges::search(ranges::begin(r1),ranges::end(r1),ranges::begin(r2),ranges::end(r2),                                   pred, proj1, proj2).empty();}}; inlineconstexpr __contains_subrange_fn contains_subrange{};

      [edit]Example

      Run this code
      #include <algorithm>#include <array>#include <complex> namespace ranges= std::ranges; int main(){constexprauto haystack=std::array{3,1,4,1,5};constexprauto needle=std::array{1,4,1};constexprauto bodkin=std::array{2,5,2};     static_assert(        ranges::contains(haystack,4)&&!ranges::contains(haystack,6)&&        ranges::contains_subrange(haystack, needle)&&!ranges::contains_subrange(haystack, bodkin)); constexprstd::array<std::complex<double>,3> nums{{{1,2},{3,4},{5,6}}};#ifdef __cpp_lib_algorithm_default_value_type        static_assert(ranges::contains(nums,{3,4}));#else        static_assert(ranges::contains(nums,std::complex<double>{3,4}));#endif}

      [edit]See also

      finds the first element satisfying specific criteria
      (algorithm function object)[edit]
      searches for the first occurrence of a range of elements
      (algorithm function object)[edit]
      determines if an element exists in a partially-ordered range
      (algorithm function object)[edit]
      returnstrue if one sequence is a subsequence of another
      (algorithm function object)[edit]
      checks if a predicate istrue for all, any or none of the elements in a range
      (algorithm function object)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/algorithm/ranges/contains&oldid=182619"

      [8]ページ先頭

      ©2009-2025 Movatter.jp