std::ranges::all_of, std::ranges::any_of, std::ranges::none_of - cppreference.com
| 在标头 |
||
| 调用签名 |
||
template< std::input_iterator I, std::sentinel_for<I> S, class Proj = std::identity, std::indirect_unary_predicate<std::projected<I, Proj>> Pred > constexpr bool all_of( I first, S last, Pred pred, Proj proj = {} ); |
(1) | (C++20 起) |
template< ranges::input_range R, class Proj = std::identity, std::indirect_unary_predicate <std::projected<ranges::iterator_t<R>, Proj>> Pred > constexpr bool all_of( R&& r, Pred pred, Proj proj = {} ); |
(2) | (C++20 起) |
template< std::input_iterator I, std::sentinel_for<I> S, class Proj = std::identity, std::indirect_unary_predicate<std::projected<I, Proj>> Pred > constexpr bool any_of( I first, S last, Pred pred, Proj proj = {} ); |
(3) | (C++20 起) |
template< ranges::input_range R, class Proj = std::identity, std::indirect_unary_predicate <std::projected<ranges::iterator_t<R>, Proj>> Pred > constexpr bool any_of( R&& r, Pred pred, Proj proj = {} ); |
(4) | (C++20 起) |
template< std::input_iterator I, std::sentinel_for<I> S, class Proj = std::identity, std::indirect_unary_predicate<std::projected<I, Proj>> Pred > constexpr bool none_of( I first, S last, Pred pred, Proj proj = {} ); |
(5) | (C++20 起) |
template< ranges::input_range R, class Proj = std::identity, std::indirect_unary_predicate <std::projected<ranges::iterator_t<R>, Proj>> Pred > constexpr bool none_of( R&& r, Pred pred, Proj proj = {} ); |
(6) | (C++20 起) |
template< /*execution-policy*/ Ep, std::random_access_iterator I, std::sized_sentinel_for<I> S, class Proj = std::identity, std::indirect_unary_predicate<std::projected<I, Proj>> Pred > bool all_of( Ep&& policy, I first, S last, Pred pred, Proj proj = {} ); |
(7) | (C++26 起) |
template< /*execution-policy*/ Ep, /*sized-random-access-range*/ R, class Proj = std::identity, std::indirect_unary_predicate <std::projected<ranges::iterator_t<R>, Proj>> Pred > bool all_of( Ep&& policy, R&& r, Pred pred, Proj proj = {} ); |
(8) | (C++26 起) |
template< /*execution-policy*/ Ep, std::random_access_iterator I, std::sized_sentinel_for<I> S, class Proj = std::identity, std::indirect_unary_predicate<std::projected<I, Proj>> Pred > bool any_of( Ep&& policy, I first, S last, Pred pred, Proj proj = {} ); |
(9) | (C++26 起) |
template< /*execution-policy*/ Ep, /*sized-random-access-range*/ R, class Proj = std::identity, std::indirect_unary_predicate <std::projected<ranges::iterator_t<R>, Proj>> Pred > bool any_of( Ep&& policy, R&& r, Pred pred, Proj proj = {} ); |
(10) | (C++26 起) |
template< /*execution-policy*/ Ep, std::random_access_iterator I, std::sized_sentinel_for<I> S, class Proj = std::identity, std::indirect_unary_predicate<std::projected<I, Proj>> Pred > bool none_of( Ep&& policy, I first, S last, Pred pred, Proj proj = {} ); |
(11) | (C++26 起) |
template< /*execution-policy*/ Ep, /*sized-random-access-range*/ R, class Proj = std::identity, std::indirect_unary_predicate <std::projected<ranges::iterator_t<R>, Proj>> Pred > bool none_of( Ep&& policy, R&& r, Pred pred, Proj proj = {} ); |
(12) | (C++26 起) |
/*execution-policy*/ 的定义见此页;/*sized-random-access-range*/ 的定义见此页。
1,2) 检查一元谓词 pred 是否对范围 [first, last) 或 r 中至少一个(以 proj 投影后的)元素返回 false。
3,4) 检查一元谓词 pred 是否对范围 [first, last) 或 r 中至少一个(以 proj 投影后的)元素返回 true。
5,6) 检查一元谓词 pred 是否不对范围 [first, last) 或 r 中任何(以 proj 投影后的)元素返回 true。
7-12) 同 (1-6),但按照 policy 执行。
此页面上描述的函数式实体是算法函数对象(非正式地称为 niebloid),即:
参数
| first, last | - | 表示要检验的元素范围的迭代器-哨位对 |
| r | - | 要检验的元素范围 |
| pred | - | 会应用到(投影后的)元素的谓词 |
| proj | - | 会应用到元素的投影 |
| policy | - | 所用的执行策略 |
返回值
范围中含有 true 元素
|
是 | 否 | ||
|---|---|---|---|---|
范围中含有 false 元素
|
是 | 否 | 是 | 否[1] |
all_of
|
false
|
true
|
false
|
true
|
any_of
|
true
|
true
|
false
|
false
|
none_of
|
false
|
false
|
true
|
true
|
- ↑ 此情况下范围为空。
复杂度
最多应用 range::distance(first, last) 或 range::distance(r) 次 pred 和 proj。
异常
可能的实现
| all_of (1,2) |
|---|
struct all_of_fn { template<std::input_iterator I, std::sentinel_for<I> S, class Proj = std::identity, std::indirect_unary_predicate<std::projected<I, Proj>> Pred> constexpr bool operator()(I first, S last, Pred pred, Proj proj = {}) const { return ranges::find_if_not(first, last, std::ref(pred), std::ref(proj)) == last; } template<ranges::input_range R, class Proj = std::identity, std::indirect_unary_predicate <std::projected<ranges::iterator_t<R>, Proj>> Pred> constexpr bool operator()(R&& r, Pred pred, Proj proj = {}) const { return operator()(ranges::begin(r), ranges::end(r), std::ref(pred), std::ref(proj)); } template<ranges::forward_range R, class Proj = std::identity, std::indirect_unary_predicate <std::projected<ranges::iterator_t<R>, Proj>> Pred> constexpr bool operator()(R&& r, Pred pred, Proj proj = {}) const { return operator()(ranges::begin(r), ranges::next(ranges::begin(r), ranges::end(r)), std::ref(pred), std::ref(proj)); } }; inline constexpr all_of_fn all_of; |
| any_of (3,4) |
struct any_of_fn { template<std::input_iterator I, std::sentinel_for<I> S, class Proj = std::identity, std::indirect_unary_predicate<std::projected<I, Proj>> Pred> constexpr bool operator()(I first, S last, Pred pred, Proj proj = {}) const { return ranges::find_if(first, last, std::ref(pred), std::ref(proj)) != last; } template<ranges::input_range R, class Proj = std::identity, std::indirect_unary_predicate <std::projected<ranges::iterator_t<R>, Proj>> Pred> constexpr bool operator()(R&& r, Pred pred, Proj proj = {}) const { return operator()(ranges::begin(r), ranges::end(r), std::ref(pred), std::ref(proj)); } template<ranges::forward_range R, class Proj = std::identity, std::indirect_unary_predicate <std::projected<ranges::iterator_t<R>, Proj>> Pred> constexpr bool operator()(R&& r, Pred pred, Proj proj = {}) const { return operator()(ranges::begin(r), ranges::next(ranges::begin(r), ranges::end(r)), std::ref(pred), std::ref(proj)); } }; inline constexpr any_of_fn any_of; |
| none_of (5,6) |
struct none_of_fn { template<std::input_iterator I, std::sentinel_for<I> S, class Proj = std::identity, std::indirect_unary_predicate<std::projected<I, Proj>> Pred> constexpr bool operator()(I first, S last, Pred pred, Proj proj = {}) const { return ranges::find_if(first, last, std::ref(pred), std::ref(proj)) == last; } template<ranges::input_range R, class Proj = std::identity, std::indirect_unary_predicate <std::projected<ranges::iterator_t<R>, Proj>> Pred> constexpr bool operator()(R&& r, Pred pred, Proj proj = {}) const { return operator()(ranges::begin(r), ranges::end(r), std::ref(pred), std::ref(proj)); } template<ranges::forward_range R, class Proj = std::identity, std::indirect_unary_predicate <std::projected<ranges::iterator_t<R>, Proj>> Pred> constexpr bool operator()(R&& r, Pred pred, Proj proj = {}) const { return operator()(ranges::begin(r), ranges::next(ranges::begin(r), ranges::end(r)), std::ref(pred), std::ref(proj)); } }; inline constexpr none_of_fn none_of; |
示例
#include <algorithm> #include <functional> #include <iostream> #include <iterator> #include <numeric> #include <vector> namespace ranges = std::ranges; constexpr bool some_of(auto&& r, auto&& pred) // 一些但非全部 { return not (ranges::all_of(r, pred) or ranges::none_of(r, pred)); } constexpr auto w = {1, 2, 3}; static_assert(!some_of(w, [](int x) { return x < 1; })); static_assert( some_of(w, [](int x) { return x < 2; })); static_assert(!some_of(w, [](int x) { return x < 4; })); int main() { std::vector<int> v(10, 2); std::partial_sum(v.cbegin(), v.cend(), v.begin()); std::cout << "这些数中:"; ranges::copy(v, std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n'; if (ranges::all_of(v.cbegin(), v.cend(), [](int i) { return i % 2 == 0; })) std::cout << "全部都是偶数\n"; if (ranges::none_of(v, std::bind(std::modulus<int>(), std::placeholders::_1, 2))) std::cout << "没有奇数\n"; auto DivisibleBy = [](int d) { return [d](int m) { return m % d == 0; }; }; if (ranges::any_of(v, DivisibleBy(7))) std::cout << "至少一个数可被 7 整除\n"; }
输出:
这些数中:2 4 6 8 10 12 14 16 18 20 全部都是偶数 没有奇数 至少一个数可被 7 整除