std::ranges::contains, std::ranges::contains_subrange
来自 zh.cppreference.net
| 定义于头文件 <algorithm>
|
||
| 调用签名 |
||
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 = {} );
|
(1) | (自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起) | |
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 = {} );
|
(2) | (自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起) |
template< /*execution-policy*/ Ep,
std::random_access_iterator I, std::sized_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*>
bool contains( Ep&& policy, I first, S last, const T& value, Proj proj = {} );
|
(5) | (自C++26起) |
template< /*execution-policy*/ Ep, /*sized-random-access-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*>
bool contains( Ep&& policy, R&& r, const T& value, Proj proj = {} );
|
(6) | (自C++26起) |
template< /*execution-policy*/ Ep,
std::random_access_iterator I1, std::sized_sentinel_for<I1> S1,
std::random_access_iterator I2, std::sized_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>
bool contains_subrange( Ep&& policy, I1 first1, S1 last1, I2 first2, S2 last2,
Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {} );
|
(7) | (自C++26起) |
template< /*execution-policy*/ Ep,
/*sized-random-access-range*/ R1, /*sized-random-access-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>
bool contains_subrange( Ep&& policy, R1&& r1, R2&& r2, Pred pred = {},
Proj1 proj1 = {}, Proj2 proj2 = {} );
|
(8) | (自C++26起) |
关于 /*execution-policy*/ 的定义,请参阅 此页面;关于 /*sized-random-access-range*/ 的定义,请参阅 此页面。
1,2) 检查源范围是否包含目标值
value。1) 源范围是
[first, last)。2) 源范围是
r。3,4) 检查目标范围是否是源范围的子范围。
3) 源范围是
[first1, last1),目标范围是 [first2, last2)。4) 源范围是
r1,目标范围是 r2。5-8) 与 (1-4) 相同,但根据
policy 执行。此页面上描述的函数式实体是 算法函数对象(非正式称为 niebloids),即:
参数
| first/first1, last/last1 | - | 定义源范围 |
| 的迭代器-哨兵对first2, last2 | - | 定义目标范围 |
| 的迭代器-哨兵对r/r1 | - | 源范围 |
| value | - | 目标值 |
| r2 | - | 目标范围 |
| pred/pred1 | - | 应用于源范围中(投影后)元素的谓词 |
| pred2 | - | 应用于目标范围中(投影后)元素的谓词 |
| proj/proj1 | - | 应用于源范围中元素的投影 |
| proj2 | - | 应用于目标范围中元素的投影 |
| policy | - | 该 执行策略 使用 |
返回值
1)
ranges::find(std::move(first), last, value, proj) != last2)
ranges::find(r, value, proj) != ranges::end(r)3)
first2 == last2 || !ranges::search(first1, last1, first2, last2, pred, proj1, proj2).empty()4)
ranges::empty(r2) || !ranges::search(r1, r2, pred, proj1, proj2).empty()复杂度
给定
- N 作为
ranges::distance(first, last)或ranges::distance(r), - N1 作为
ranges::distance(first1, last1)或ranges::distance(r1),并且 - N2 作为
ranges::distance(first2, last2)或ranges::distance(r2):
1,2) 至多 N 次比较和调用
proj。
3,4) 至多 N1·N2 次调用
pred 和 proj。
5,6) 𝓞(N) 次比较和调用
proj。
7,8) 𝓞(N1·N2) 次调用
pred 和 proj。
异常
5-8)在执行过程中:
- 如果并行化所需的临时内存资源不可用,则抛出std::bad_alloc。
- 如果通过算法参数访问对象时抛出未捕获的异常,则行为由执行策略决定(对于标准策略,将调用std::terminate)。
注解
在 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(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 ranges::empty(r2) ||
!ranges::search(r1, 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 }
另请参阅
(C++20)(C++20)(C++20) |
找到满足特定条件的第一个元素 (算法函数对象) |
(C++20) |
搜索某个元素范围的首次出现 (算法函数对象) |
(C++20) |
使用二分搜索确定元素是否存在于范围中 (算法函数对象) |
(C++20) |
确定一个序列是否为另一个序列的子序列 (算法函数对象) |
(C++20)(C++20)(C++20) |
检查谓词是否 true 对范围中的全部、任一或无元素(算法函数对象) |