std::ranges:: contains, std::ranges:: contains_subrange
|
定义于头文件
<algorithm>
|
||
|
调用签名
|
||
| (1) | ||
|
template
<
std::
input_iterator
I,
std::
sentinel_for
<
I
>
S,
class
T,
|
(C++23 起)
(C++26 前) |
|
|
template
<
std::
input_iterator
I,
std::
sentinel_for
<
I
>
S,
class
Proj
=
std::
identity
,
|
(C++26 起) | |
| (2) | ||
|
template
<
ranges::
input_range
R,
class
T,
|
(C++23 起)
(C++26 前) |
|
|
template
<
ranges::
input_range
R,
class
Proj
=
std::
identity
,
|
(C++26 起) | |
|
template
<
std::
forward_iterator
I1,
std::
sentinel_for
<
I1
>
S1,
std::
forward_iterator
I2,
std::
sentinel_for
<
I2
>
S2,
|
(3) | (C++23 起) |
|
template
<
ranges::
forward_range
R1,
ranges::
forward_range
R2,
class
Pred
=
ranges::
equal_to
,
|
[
first
,
last
)
。
[
ranges::
begin
(
r
)
,
ranges::
end
(
r
)
)
。
[
first1
,
last1
)
,第二个源范围是
[
first2
,
last2
)
。
[
ranges::
begin
(
r1
)
,
ranges::
end
(
r1
)
)
,第二个源范围是
[
ranges::
begin
(
r2
)
,
ranges::
end
(
r2
)
)
。
本页面描述的函数式实体是 算法函数对象 (非正式称为 niebloids ),即:
目录 |
参数
| first, last | - | 定义待检验元素范围的 区间 的迭代器-哨位对 |
| r | - | 待检验元素的范围 |
| value | - | 用于与元素比较的值 |
| pred | - | 应用于投影元素的谓词 |
| proj | - | 应用于元素的投影 |
返回值
! ranges:: search ( ranges:: begin ( r1 ) , ranges:: end ( r1 ) ,
ranges:: begin ( r2 ) , ranges:: end ( r2 ) , pred, proj1, proj2 ) . empty ( )
复杂度
注释
在C++20中,可以通过 contains 函数配合 ranges:: find ( haystack, needle ) ! = ranges:: end ( haystack ) 来实现包含检测,或通过 contains_subrange 配合 ! ranges:: search ( haystack, needle ) . empty ( ) 实现子范围包含检测。
ranges::contains_subrange
与
ranges::search
类似,但不同于
std::search
,它不支持
搜索器
(例如
std::boyer_moore_searcher
)。
| 功能测试 宏 | 值 | 标准 | 功能 |
|---|---|---|---|
__cpp_lib_ranges_contains
|
202207L
|
(C++23) |
ranges::contains
和
ranges::contains_subrange
|
__cpp_lib_algorithm_default_value_type
|
202403L
|
(C++26) | 算法的列表初始化 ( 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 |
示例
#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 }
参见
|
(C++20)
(C++20)
(C++20)
|
查找满足特定条件的首个元素
(算法函数对象) |
|
(C++20)
|
搜索元素范围的首次出现
(算法函数对象) |
|
(C++20)
|
判断元素是否存在于部分有序范围中
(算法函数对象) |
|
(C++20)
|
若一个序列是另一个序列的子序列则返回
true
(算法函数对象) |
|
(C++20)
(C++20)
(C++20)
|
检查谓词是否对范围中所有、任一或无元素返回
true
(算法函数对象) |