|
|
|
调用签名
|
|
|
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 起) |
|
|
|
1) 检查一元谓词 pred
是否对范围 [first, last)
中的所有元素(以投影 proj
投影后)返回 true 。
3) 检查一元谓词 pred
是否对范围 [first, last)
中至少一个元素(以投影 proj
投影后)返回 true 。
5) 检查一元谓词 pred
是否不对范围 [first, last)
中任何元素(以投影 proj
投影后)返回 true 。
2,4,6) 同 (1,3,5) ,但以 r
为源范围,如同以 ranges::begin(r) 为
first
并以 ranges::end(r) 为 last
。
此页面上描述的仿函数实体是 niebloid,即:
实际上,它们能以函数对象,或者某些特殊编译器扩展实现。
参数
first, last
|
-
|
要检验的元素范围
|
r
|
-
|
要检验的元素范围
|
pred
|
-
|
应用到投影后元素的谓词
|
proj
|
-
|
应用到元素的投影
|
返回值
3-4) 若
std::invoke(pred, std::invoke(proj, *i)) != false
对范围中的至少一个迭代器
i
成立则为
true ,否则为
false 。若范围为空则返回
false 。
复杂度
至多应用 last - first
次谓词和投影。
可能的实现
版本一
|
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;
|
版本二
|
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;
|
版本三
|
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;
|
示例
#include <vector>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <iostream>
#include <functional>
namespace ranges = std::ranges;
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
参阅
|
检查谓词是否对范围中所有、任一或无元素为 true (函数模板) |