std::ranges::all_of,std::ranges::any_of,std::ranges::none_of
定义在头文件 <algorithm> 中 |
||
调用签名 |
||
template< std::input_iterator I, std::sentinel_for<I> S, class Proj = std::identity, |
(1) | (自 C++20 起) |
template< ranges::input_range R, class Proj = std::identity, std::indirect_unary_predicate< |
(2) | (自 C++20 起) |
template< std::input_iterator I, std::sentinel_for<I> S, class Proj = std::identity, |
(3) | (自 C++20 起) |
template< ranges::input_range R, class Proj = std::identity, std::indirect_unary_predicate< |
(4) | (自 C++20 起) |
模板< std::输入迭代器 I, std::哨兵<I> S, 类 Proj = std::标识, constexpr 布尔 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< |
(6) | (自 C++20 起) |
[
first,
last)
中的所有元素返回 true (在用投影 proj 投影后)。[
first,
last)
中的至少一个元素返回 true (在用投影 proj 投影后)。[
first,
last)
中的任何元素都不返回 true (在用投影 proj 投影后)。此页面上描述的类似函数的实体是 niebloids,即
在实践中,它们可以用函数对象来实现,或者用特殊的编译器扩展来实现。
内容 |
[编辑] 参数
first, last | - | 要检查的元素的范围 |
r | - | 要检查的元素的范围 |
pred | - | 要应用于投影元素的谓词 |
proj | - | 要应用于元素的投影 |
[编辑] 返回值
另见 注意。
[编辑] 复杂度
谓词和投影最多应用 last - first 次。
[编辑] 可能的实现
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)); } }; 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)); } }; 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)); } }; inline constexpr none_of_fn none_of; |
[编辑] 注意
输入范围包含 | ||||
---|---|---|---|---|
所有 true, 没有 false |
一些 true, 一些 false |
没有 true, 所有 false |
没有 true, 没有 false (空范围) | |
1,2) all_of | true | false | false | true |
3,4) any_of | true | true | false | false |
5,6) none_of | false | false | true | true |
[编辑] 示例
#include <algorithm> #include <functional> #include <iostream> #include <iterator> #include <numeric> #include <vector> namespace ranges = std::ranges; constexpr bool some_of(auto&& r, auto&& pred) // some but not all { 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 << "Among the numbers: "; 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 << "All numbers are even\n"; if (ranges::none_of(v, std::bind(std::modulus<int>(), std::placeholders::_1, 2))) std::cout << "None of them are odd\n"; auto DivisibleBy = [](int d) { return [d](int m) { return m % d == 0; }; }; if (ranges::any_of(v, DivisibleBy(7))) std::cout << "At least one number is divisible by 7\n"; }
输出
Among the numbers: 2 4 6 8 10 12 14 16 18 20 All numbers are even None of them are odd At least one number is divisible by 7
[编辑] 参见
(C++11)(C++11)(C++11) |
检查谓词是否对范围中的所有元素、任何元素或没有元素都返回 true (函数模板) |