Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/algorithm/ranges/contains"

From cppreference.com
< cpp‎ | algorithm‎ | ranges
m (Notes: ~FTM)
m (FTM.)
 
(10 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{cpp/ranges/title| contains | contains_subrange}}
+
{{cpp/ranges/title|contains|contains_subrange}}
 
{{cpp/algorithm/ranges/navbar}}
 
{{cpp/algorithm/ranges/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl header | algorithm}}
+
{{dcl header|algorithm}}
{{dcl h | Call signature}}
+
{{dcl h|Call signature}}
{{dcl | since=c++23 | num= 1 |1=
+
{{dcl rev multi|num=1|anchor=1|since1=c++23|until1=c++26|dcl1=
 
template< std::input_iterator I, std::sentinel_for<I> S,
 
template< std::input_iterator I, std::sentinel_for<I> S,
           class T, class Proj = std::identity >
+
           class T,
requires std::indirect_binary_predicate<ranges::equal_to, std::projected<I, Proj>,  
+
          class Proj = std::identity >
 +
requires std::indirect_binary_predicate<ranges::equal_to, std::projected<I, Proj>,
 +
                                        const T*>
 +
constexpr bool contains( I first, S last, const T& value, Proj proj = {} );
 +
|dcl2=
 +
template< std::input_iterator I, std::sentinel_for<I> S,
 +
          class Proj = std::identity,
 +
          class T = std::projected_value_t<I, Proj> >
 +
requires std::indirect_binary_predicate<ranges::equal_to, std::projected<I, Proj>,
 
                                         const T*>
 
                                         const T*>
 
constexpr bool contains( I first, S last, const T& value, Proj proj = {} );
 
constexpr bool contains( I first, S last, const T& value, Proj proj = {} );
 
}}
 
}}
{{dcl | since=c++23 | num= 2 |1=
+
{{dcl rev multi|num=2|since1=c++23|until1=c++26|dcl1=
template< ranges::input_range R, class T, class Proj = std::identity >
+
template< ranges::input_range R,
 +
          class T,
 +
          class Proj = std::identity >
 +
requires std::indirect_binary_predicate<ranges::equal_to,
 +
                                        std::projected<ranges::iterator_t<R>, Proj>,
 +
                                        const T*>
 +
constexpr bool contains( R&& r, const T& value, Proj proj = {} );
 +
|dcl2=
 +
template< ranges::input_range R,
 +
          class Proj = std::identity,
 +
          class T = std::projected_value_t<ranges::iterator_t<R>, Proj> >
 
requires std::indirect_binary_predicate<ranges::equal_to,
 
requires std::indirect_binary_predicate<ranges::equal_to,
 
                                         std::projected<ranges::iterator_t<R>, Proj>,
 
                                         std::projected<ranges::iterator_t<R>, Proj>,
Line 18: Line 36:
 
constexpr bool contains( R&& r, const T& value, Proj proj = {} );
 
constexpr bool contains( R&& r, const T& value, Proj proj = {} );
 
}}
 
}}
{{dcl | since=c++23 | num= 3 |1=
+
{{dcla|since=c++23|num=3|1=
 
template< std::forward_iterator I1, std::sentinel_for<I1> S1,
 
template< std::forward_iterator I1, std::sentinel_for<I1> S1,
 
           std::forward_iterator I2, std::sentinel_for<I2> S2,
 
           std::forward_iterator I2, std::sentinel_for<I2> S2,
Line 24: Line 42:
 
           class Proj1 = std::identity, class Proj2 = std::identity >
 
           class Proj1 = std::identity, class Proj2 = std::identity >
 
requires std::indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
 
requires std::indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
constexpr bool contains_subrange( I1 first1, S1 last1,
+
constexpr bool contains_subrange( I1 first1, S1 last1, I2 first2, S2 last2,
                                  I2 first2, S2 last2,
+
 
                                   Pred pred = {},
 
                                   Pred pred = {},
 
                                   Proj1 proj1 = {}, Proj2 proj2 = {} );
 
                                   Proj1 proj1 = {}, Proj2 proj2 = {} );
 
}}
 
}}
{{dcl | since=c++23 | num= 4 |1=
+
{{dcl|since=c++23|num=4|1=
 
template< ranges::forward_range R1, ranges::forward_range R2,
 
template< ranges::forward_range R1, ranges::forward_range R2,
 
           class Pred = ranges::equal_to,
 
           class Pred = ranges::equal_to,
 
           class Proj1 = std::identity, class Proj2 = std::identity >
 
           class Proj1 = std::identity, class Proj2 = std::identity >
requires std::indirectly_comparable<ranges::iterator_t<R1>,  
+
requires std::indirectly_comparable<ranges::iterator_t<R1>,
 
                                     ranges::iterator_t<R2>, Pred, Proj1, Proj2>
 
                                     ranges::iterator_t<R2>, Pred, Proj1, Proj2>
constexpr bool contains_subrange( R1&& r1, R2&& r2,
+
constexpr bool contains_subrange( R1&& r1, R2&& r2, Pred pred = {},
                                  Pred pred = {},
+
 
                                   Proj1 proj1 = {}, Proj2 proj2 = {} );
 
                                   Proj1 proj1 = {}, Proj2 proj2 = {} );
 
}}
 
}}
Line 42: Line 58:
  
 
@1@ Search-based algorithm that checks whether or not a given range contains a value with iterator-sentinel pairs.
 
@1@ Search-based algorithm that checks whether or not a given range contains a value with iterator-sentinel pairs.
@2@ Same as {{v|1}} but uses {{tt|r}} as the source range, as if using {{c|ranges::begin(r)}} as {{tt|first}} and {{c|ranges::end(r)}} as {{tt|last}}.
+
@2@ Same as {{v|1}} but uses {{c|r}} as the source range, as if using {{c|ranges::begin(r)}} as {{c|first}} and {{c|ranges::end(r)}} as {{c|last}}.
 
@3@ Search-based algorithm that checks whether or not a given range is a subrange of another range with iterator-sentinel pairs.
 
@3@ Search-based algorithm that checks whether or not a given range is a subrange of another range with iterator-sentinel pairs.
@4@ Same as {{v|3}} but uses {{tt|r1}} as the first source range and {{tt|r2}} as the second source range, as if using {{c|ranges::begin(r1)}} as {{tt|first1}}, {{c|ranges::end(r1)}} as {{tt|last1}}, {{c|ranges::begin(r2)}} as {{tt|first2}}, and {{c|ranges::end(r2)}} as {{tt|last2}}.
+
@4@ Same as {{v|3}} but uses {{c|r1}} as the first source range and {{c|r2}} as the second source range, as if using {{c|ranges::begin(r1)}} as {{c|first1}}, {{c|ranges::end(r1)}} as {{c|last1}}, {{c|ranges::begin(r2)}} as {{c|first2}}, and {{c|ranges::end(r2)}} as {{c|last2}}.
  
 
{{cpp/ranges/niebloid}}
 
{{cpp/ranges/niebloid}}
  
=== Parameters ===
+
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | first, last | the range of elements to examine}}
+
{{par|first, last|the range of elements to examine}}
{{par | r | the range of the elements to examine}}
+
{{par|r|the range of the elements to examine}}
{{par | value | value to compare the elements to}}
+
{{par|value|value to compare the elements to}}
{{par | pred | predicate to apply to the projected elements}}
+
{{par|pred|predicate to apply to the projected elements}}
{{par | proj | projection to apply to the elements}}
+
{{par|proj|projection to apply to the elements}}
 
{{par end}}
 
{{par end}}
  
=== Return value ===
+
===Return value===
@1-2@: {{c|1= ranges::find(std::move(first), last, value, proj) != last}}
+
@1,2@: {{c|1=ranges::find(std::move(first), last, value, proj) != last}}
  
@3-4@: {{c|1= first2 == last2 {{!!}} !ranges::search(first1, last1, first2, last2, pred, proj1, proj2).empty()}}
+
@3,4@: {{c|1=first2 == last2 {{!!}} !ranges::search(first1, last1, first2, last2, pred, proj1, proj2).empty()}}
  
=== Complexity ===
+
===Complexity===
At most {{tt|last}} - {{tt|first}} applications of the predicate and projection.
+
At most {{c|last - first}} applications of the predicate and projection.
  
=== Notes ===
+
===Notes===
 
Up until C++20, we've had to write {{c|1= std::ranges::find(r, value) != std::ranges::end(r)}} to determine if a single value is inside a range. And to check if a range contains a subrange of interest, we use {{c|1= not std::ranges::search(haystack, needle).empty()}}. While this is accurate, it isn't necessarily convenient, and it hardly expresses intent (especially in the latter case). Being able to say {{c|std::ranges::contains(r, value)}} addresses both of these points.
 
Up until C++20, we've had to write {{c|1= std::ranges::find(r, value) != std::ranges::end(r)}} to determine if a single value is inside a range. And to check if a range contains a subrange of interest, we use {{c|1= not std::ranges::search(haystack, needle).empty()}}. While this is accurate, it isn't necessarily convenient, and it hardly expresses intent (especially in the latter case). Being able to say {{c|std::ranges::contains(r, value)}} addresses both of these points.
  
 
{{tt|ranges::contains_subrange}}, same as {{lc|ranges::search}}, but as opposed to {{lc|std::search}}, provides no access to {{named req|Searcher}}s (such as [[cpp/utility/functional#Searchers|Boyer-Moore]]).
 
{{tt|ranges::contains_subrange}}, same as {{lc|ranges::search}}, but as opposed to {{lc|std::search}}, provides no access to {{named req|Searcher}}s (such as [[cpp/utility/functional#Searchers|Boyer-Moore]]).
{{feature test macro|__cpp_lib_ranges_contains|value=202207L|std=C++23}}
 
  
=== Possible implementation ===
+
{{ftm begin}}
{{eq fun
+
{{ftm|__cpp_lib_ranges_contains|value=202207L|std=C++23|{{ttt|std::ranges::contains}} and {{ttt|ranges::contains_subrange}}}}
| 1=
+
{{ftm|__cpp_lib_algorithm_default_value_type|value=202403|std=C++26|[[cpp/language/list initialization|List-initialization]] for algorithms {{vl|1,2}}}}
 +
{{ftm end}}
 +
 
 +
===Possible implementation===
 +
{{eq impl
 +
|title1=contains (1,2)|ver1=1|1=
 
struct __contains_fn
 
struct __contains_fn
 
{
 
{
     template< std::input_iterator I, std::sentinel_for<I> S,
+
     template<std::input_iterator I, std::sentinel_for<I> S,
              class T, class Proj = std::identity >
+
            class Proj = std::identity,
     requires std::indirect_binary_predicate<ranges::equal_to, std::projected<I, Proj>,  
+
            class T = std::projected_value_t<I, Proj>>
 +
     requires std::indirect_binary_predicate<ranges::equal_to, std::projected<I, Proj>,
 
                                             const T*>
 
                                             const T*>
 
     constexpr bool operator()(I first, S last, const T& value, Proj proj = {}) const
 
     constexpr bool operator()(I first, S last, const T& value, Proj proj = {}) const
Line 85: Line 106:
 
     }
 
     }
  
     template< ranges::input_range R, class T, class Proj = std::identity >
+
     template<ranges::input_range R,
 +
            class Proj = std::identity,
 +
            class T = std::projected_value_t<ranges::iterator_t<R>, Proj>>
 
     requires std::indirect_binary_predicate<ranges::equal_to,
 
     requires std::indirect_binary_predicate<ranges::equal_to,
 
                                             std::projected<ranges::iterator_t<R>, Proj>,
 
                                             std::projected<ranges::iterator_t<R>, Proj>,
Line 96: Line 119:
  
 
inline constexpr __contains_fn contains {};
 
inline constexpr __contains_fn contains {};
| 2=
+
|title2=contains_subrange (3,4)|ver2=3|2=
 
struct __contains_subrange_fn
 
struct __contains_subrange_fn
 
{
 
{
     template< std::forward_iterator I1, std::sentinel_for<I1> S1,
+
     template<std::forward_iterator I1, std::sentinel_for<I1> S1,
              std::forward_iterator I2, std::sentinel_for<I2> S2,
+
            std::forward_iterator I2, std::sentinel_for<I2> S2,
              class Pred = ranges::equal_to,
+
            class Pred = ranges::equal_to,
              class Proj1 = std::identity, class Proj2 = std::identity >
+
            class Proj1 = std::identity, class Proj2 = std::identity>
 
     requires std::indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
 
     requires std::indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
 
     constexpr bool operator()(I1 first1, S1 last1,
 
     constexpr bool operator()(I1 first1, S1 last1,
Line 113: Line 136:
 
     }
 
     }
  
     template< ranges::forward_range R1, ranges::forward_range R2,
+
     template<ranges::forward_range R1, ranges::forward_range R2,
              class Pred = ranges::equal_to,
+
            class Pred = ranges::equal_to,
              class Proj1 = std::identity, class Proj2 = std::identity >
+
            class Proj1 = std::identity, class Proj2 = std::identity>
     requires std::indirectly_comparable<ranges::iterator_t<R1>,  
+
     requires std::indirectly_comparable<ranges::iterator_t<R1>,
 
                                         ranges::iterator_t<R2>, Pred, Proj1, Proj2>
 
                                         ranges::iterator_t<R2>, Pred, Proj1, Proj2>
 
     constexpr bool operator()(R1&& r1, R2&& r2,
 
     constexpr bool operator()(R1&& r1, R2&& r2,
Line 131: Line 154:
 
}}
 
}}
  
=== Example ===
+
===Example===
{{example|code=
+
{{example
 +
|code=
 
#include <algorithm>
 
#include <algorithm>
 
#include <array>
 
#include <array>
 +
#include <complex>
 +
 +
namespace ranges = std::ranges;
  
 
int main()
 
int main()
 
{
 
{
     constexpr auto haystack = std::array { 3, 1, 4, 1, 5 };
+
     constexpr auto haystack = std::array{3, 1, 4, 1, 5};
     constexpr auto needle = std::array { 1, 4, 1 };
+
     constexpr auto needle = std::array{1, 4, 1};
     constexpr auto bodkin = std::array { 2, 5, 2 };
+
     constexpr auto bodkin = std::array{2, 5, 2};
    auto increment = [](int x) { return ++x; };
+
    auto decrement = [](int x) { return --x; };
+
  
 
     static_assert(
 
     static_assert(
            std::ranges::contains(haystack, 4) and
+
        ranges::contains(haystack, 4) &&
        not std::ranges::contains(haystack, 6) and
+
      !ranges::contains(haystack, 6) &&
            std::ranges::contains_subrange(haystack, needle) and
+
        ranges::contains_subrange(haystack, needle) &&
        not std::ranges::contains_subrange(haystack, bodkin) and
+
      !ranges::contains_subrange(haystack, bodkin)
            std::ranges::contains(haystack, 6, increment) and
+
    );
        not std::ranges::contains(haystack, 1, increment) and
+
 
            std::ranges::contains_subrange(haystack, bodkin, {}, increment) and
+
    constexpr std::array<std::complex<double>, 3> nums{<!---->{<!---->{1, 2}, {3, 4}, {5, 6}<!---->}<!---->};
         not std::ranges::contains_subrange(haystack, bodkin, {}, decrement) and
+
    #ifdef __cpp_lib_algorithm_default_value_type
            std::ranges::contains_subrange(haystack, bodkin, {}, {}, decrement)
+
         static_assert(ranges::contains(nums, {3, 4}));
        );
+
    #else
 +
        static_assert(ranges::contains(nums, std::complex<double>{3, 4}));
 +
    #endif
 
}
 
}
 
}}
 
}}
  
=== See also ===  
+
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/algorithm/ranges/dsc find}}
+
{{dsc inc|cpp/algorithm/ranges/dsc find}}
{{dsc inc | cpp/algorithm/ranges/dsc search}}
+
{{dsc inc|cpp/algorithm/ranges/dsc search}}
{{dsc inc | cpp/algorithm/ranges/dsc binary_search}}
+
{{dsc inc|cpp/algorithm/ranges/dsc binary_search}}
{{dsc inc | cpp/algorithm/ranges/dsc includes}}
+
{{dsc inc|cpp/algorithm/ranges/dsc includes}}
{{dsc inc | cpp/algorithm/ranges/dsc all_any_none_of}}
+
{{dsc inc|cpp/algorithm/ranges/dsc all_any_none_of}}
 
{{dsc end}}
 
{{dsc end}}
  
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 22:44, 20 May 2024

 
 
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy, ranges::sort, ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
(C++11)                (C++11)(C++11)

Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17)(C++11)
(C++20)(C++20)
Sampling operations
(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 namespace std::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
Numeric operations
(C++23)            
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 >
requires std::indirect_binary_predicate<ranges::equal_to, std::projected<I, Proj>,
                                        const T*>

constexpr bool 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> >
requires std::indirect_binary_predicate<ranges::equal_to, std::projected<I, Proj>,
                                        const T*>

constexpr bool 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 >
requires std::indirect_binary_predicate<ranges::equal_to,
                                        std::projected<ranges::iterator_t<R>, Proj>,
                                        const T*>

constexpr bool 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> >
requires std::indirect_binary_predicate<ranges::equal_to,
                                        std::projected<ranges::iterator_t<R>, Proj>,
                                        const T*>

constexpr bool 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 >
requires std::indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
constexpr bool 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 >
requires std::indirectly_comparable<ranges::iterator_t<R1>,
                                    ranges::iterator_t<R2>, Pred, Proj1, Proj2>
constexpr bool contains_subrange( R1&& r1, R2&& r2, Pred pred = {},

                                  Proj1 proj1 = {}, Proj2 proj2 = {} );
(4) (since C++23)
1) Search-based algorithm that checks whether or not a given range contains a value with iterator-sentinel pairs.
2) Same as (1) but uses r as the source range, as if using ranges::begin(r) as first and ranges::end(r) as last.
3) Search-based algorithm that checks whether or not a given range is a subrange of another range with iterator-sentinel pairs.
4) Same as (3) but uses r1 as the first source range and r2 as the second source range, as if using ranges::begin(r1) as first1, ranges::end(r1) as last1, ranges::begin(r2) as first2, and ranges::end(r2) as last2.

The function-like entities described on this page are niebloids, that is:

In practice, they may be implemented as function objects, or with special compiler extensions.

Contents

[edit] Parameters

first, last - the range 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,2) : ranges::find(std::move(first), last, value, proj) != last
3,4) : first2 == last2 || !ranges::search(first1, last1, first2, last2, pred, proj1, proj2).empty()

[edit] Complexity

At most last - first applications of the predicate and projection.

[edit] Notes

Up until C++20, we've had to write std::ranges::find(r, value) != std::ranges::end(r) to determine if a single value is inside a range. And to check if a range contains a subrange of interest, we use not std::ranges::search(haystack, needle).empty(). While this is accurate, it isn't necessarily convenient, and it hardly expresses intent (especially in the latter case). Being able to say std::ranges::contains(r, value) addresses both of these points.

ranges::contains_subrange, same as ranges::search, but as opposed to std::search, provides no access to Searchers (such as Boyer-Moore).

Feature-test macro Value Std Feature
__cpp_lib_ranges_contains 202207L (C++23) std::ranges::contains and ranges::contains_subrange
__cpp_lib_algorithm_default_value_type 202403 (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>>
    requires std::indirect_binary_predicate<ranges::equal_to, std::projected<I, Proj>,
                                            const T*>
    constexpr bool operator()(I first, S last, const T& value, Proj proj = {}) const
    {
        return ranges::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>>
    requires std::indirect_binary_predicate<ranges::equal_to,
                                            std::projected<ranges::iterator_t<R>, Proj>,
                                            const T*>
    constexpr bool operator()(R&& r, const T& value, Proj proj = {}) const
    {
        return (*this)(ranges::begin(r), ranges::end(r), std::move(value), proj);
    }
};
 
inline constexpr __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>
    requires std::indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
    constexpr bool 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>
    requires std::indirectly_comparable<ranges::iterator_t<R1>,
                                        ranges::iterator_t<R2>, Pred, Proj1, Proj2>
    constexpr bool operator()(R1&& r1, R2&& r2,
                              Pred pred = {},
                              Proj1 proj1 = {}, Proj2 proj2 = {}) const
    {
        return (*this)(ranges::begin(r1), ranges::end(r1),
                       ranges::begin(r2), ranges::end(r2), std::move(pred),
                       std::move(proj1), std::move(proj2));
    }
};
 
inline constexpr __contains_subrange_fn contains_subrange {};

[edit] Example

#include <algorithm>
#include <array>
#include <complex>
 
namespace ranges = std::ranges;
 
int main()
{
    constexpr auto haystack = std::array{3, 1, 4, 1, 5};
    constexpr auto needle = std::array{1, 4, 1};
    constexpr auto 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)
    );
 
    constexpr std::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
(niebloid)[edit]
searches for the first occurrence of a range of elements
(niebloid)[edit]
determines if an element exists in a partially-ordered range
(niebloid)[edit]
returns true if one sequence is a subsequence of another
(niebloid)[edit]
checks if a predicate is true for all, any or none of the elements in a range
(niebloid)[edit]