std::ranges::contains, std::ranges::contains_subrange
在标头 <algorithm> 定义
|
||
调用签名 |
||
template< std::input_iterator I, std::sentinel_for<I> S, class T, class Proj = std::identity > |
(1) | (C++23 起) |
template< ranges::input_range R, class T, class Proj = std::identity > requires std::indirect_binary_predicate<ranges::equal_to, |
(2) | (C++23 起) |
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, |
(4) | (C++23 起) |
此页面上描述的仿函数实体是 niebloid,即:
实际上,它们能以函数对象,或者某些特殊编译器扩展实现。
参数
first, last | - | 要检查的元素的范围 |
r | - | 要检查的元素的范围 |
value | - | 和元素进行比较的值 |
pred | - | 适用于投影元素的谓词 |
proj | - | 应用到元素的投影 |
返回值
复杂度
谓词和投影最多应用 last - first 次。
注解
在 C++20 之前,我们必须编写 std::ranges::find(r, value) != std::ranges::end(r) 来确定单个值是否在范围内。而要检查范围是否包含感兴趣的子范围,我们使用 not std::ranges::search(haystack, needle).empty()} 。这样做虽然准确,但并不一定方便,也很难表达意图(尤其是在后一种情况下)。如果用 std::ranges::contains(r, value) 可以解决这两个问题。
ranges::contains_subrange
和 ranges::search 类似,但和 std::search 不同,不提供对 搜索器 (Searcher) (例如 Boyer-Moore)的支持。
功能特性测试宏 | 值 | 标准 | 备注 |
---|---|---|---|
__cpp_lib_ranges_contains |
202207L | (C++23) | std::ranges::contains 和 ranges::contains_subrange
|
可能的实现
contains |
---|
struct __contains_fn { 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 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 T, class Proj = std::identity> 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 |
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 {}; |
示例
#include <algorithm> #include <array> 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}; auto increment = [](int x) { return ++x; }; auto decrement = [](int x) { return --x; }; static_assert( std::ranges::contains(haystack, 4) && !std::ranges::contains(haystack, 6) && std::ranges::contains_subrange(haystack, needle) && !std::ranges::contains_subrange(haystack, bodkin) && std::ranges::contains(haystack, 6, increment) && !std::ranges::contains(haystack, 1, increment) && std::ranges::contains_subrange(haystack, bodkin, {}, increment) && !std::ranges::contains_subrange(haystack, bodkin, {}, decrement) && std::ranges::contains_subrange(haystack, bodkin, {}, {}, decrement) ); }
参阅
(C++20)(C++20)(C++20) |
查找满足特定条件的的第一个元素 (niebloid) |
(C++20) |
搜索一个元素范围 (niebloid) |
(C++20) |
确定元素是否存在于某范围中 (niebloid) |
(C++20) |
若一个序列是另一个的子列则返回 true (niebloid) |
(C++20)(C++20)(C++20) |
检查谓词是否对范围中所有、任一或无元素为 true (niebloid) |