来自cppreference.com
| 在标头 <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 执行。此页面上描述的函数式实体是算法函数对象(非正式地称为 niebloid),即:
参数
| 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()复杂度
给定
- \(\scriptsize N\)N 为
ranges::distance(first, last)或ranges::distance(r), - \(\scriptsize N_1\)N1 为
ranges::distance(first1, last1)或ranges::distance(r1),以及 - \(\scriptsize N_2\)N2 为
ranges::distance(first2, last2)或ranges::distance(r2):
1,2) 最多进行 \(\scriptsize N\)N 次比较和应用
proj。3,4) 最多应用 \(\scriptsize N_1 \cdot N_2\)N1·N2 次
pred 和 proj。5,6) 进行 \(\scriptsize \mathcal{O}(N)\)𝓞(N) 次比较和应用
proj。7,8) 最多应用 \(\scriptsize \mathcal{O}(N_1 \cdot N_2)\)𝓞(N1·N2) 次
pred 和 proj。注解
在 C++20 中可以分别通过 ranges::find(r, value) != ranges::end(r) 和 !std::ranges::search(haystack, needle).empty() 来实现 contains 和 contains_subrange。
ranges::contains_subrange 和 ranges::search 类似,但和 std::search 不同,不提供对 搜索器(例如 Boyer-Moore)的支持。
| 功能特性测试宏 | 值 | 标准 | 功能特性 |
|---|---|---|---|
__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 (算法函数对象) |