Namespaces
Variants

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

From cppreference.net
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
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
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
Operations on uninitialized storage
Return types
헤더 파일에 정의됨 <algorithm>
호출 시그니처
(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 = { } ) ;
(C++23부터)
(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 = { } ) ;
(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 = { } ) ;
(C++23부터)
(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 = { } ) ;
(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) (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) (C++23부터)
1,2) 주어진 범위가 값 value 를 포함하는지 여부를 검사합니다.
1) 소스 범위는 [ first , last ) 입니다.
2) 소스 범위는 [ ranges:: begin ( r ) , ranges:: end ( r ) ) 입니다.
3) 주어진 범위가 다른 범위의 하위 범위인지 여부를 확인합니다.
3) 첫 번째 소스 범위는 [ first1 , last1 ) 이고, 두 번째 소스 범위는 [ first2 , last2 ) 입니다.
4) 첫 번째 소스 범위는 [ ranges:: begin ( r1 ) , ranges:: end ( r1 ) ) 이고, 두 번째 소스 범위는 [ ranges:: begin ( r2 ) , ranges:: end ( r2 ) ) 입니다.

이 페이지에서 설명하는 함수형 개체들은 algorithm function objects (일반적으로 niebloids 로 알려진)입니다. 즉:

목차

매개변수

first, last - 검사할 요소들의 범위 를 정의하는 반복자-감시자 쌍
r - 검사할 요소들의 범위
value - 요소들과 비교할 값
pred - 투영된 요소들에 적용할 predicate
proj - 요소들에 적용할 projection

반환값

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 ( )

복잡도

1) 최대 ranges:: distance ( first, last ) 번의 비교.
2) 최대 ranges:: distance ( r ) 번의 비교.
3) 최대 ranges:: distance ( first1, last1 ) * ranges:: distance ( first2, last2 ) 번의 비교.
4) 최대 ranges:: distance ( r1 ) * ranges:: distance ( r2 ) 번의 비교.

참고 사항

C++20에서는 contains 함수를 ranges:: find ( haystack, needle ) ! = ranges:: end ( haystack ) 로 구현할 수 있으며, contains_subrange ! ranges:: search ( haystack, needle ) . empty ( ) 로 구현할 수 있습니다.

ranges::contains_subrange ranges::search 와 유사하게, 그리고 std::search 와는 달리, searchers (예: std::boyer_moore_searcher )에 대한 지원을 포함하지 않습니다.

Feature-test macro Value Std Feature
__cpp_lib_ranges_contains 202207L (C++23) ranges::contains ranges::contains_subrange
__cpp_lib_algorithm_default_value_type 202403L (C++26) 목록 초기화 for algorithms ( 1,2 )

가능한 구현

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 ranges::find(std::move(ranges::begin(r)),
                            ranges::end(r), value, proj) != ranges::end(r);
    }
};
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 (first2 == last2) ||
                   !ranges::search(ranges::begin(r1), ranges::end(r1),
                                   ranges::begin(r2), ranges::end(r2),
                                   pred, proj1, proj2).empty();
    }
};
inline constexpr __contains_subrange_fn contains_subrange{};

예제

#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
}

참고 항목

특정 조건을 만족하는 첫 번째 요소를 찾음
(알고리즘 함수 객체)
요소 범위의 첫 번째 발생을 검색함
(알고리즘 함수 객체)
부분적으로 정렬된 범위에 요소가 존재하는지 확인함
(알고리즘 함수 객체)
한 시퀀스가 다른 시퀀스의 부분 시퀀스인 경우 true 를 반환함
(알고리즘 함수 객체)
범위 내 모든 요소, 어떤 요소, 또는 어떤 요소도 조건자를 만족하는지 확인함
(알고리즘 함수 객체)