std::ranges::none_of() 算法
- 自 C++20 起
- 简化
- 详细
// (1)
constexpr bool none_of( I first, S last, Pred pred, Proj proj = {} );
// (2)
constexpr bool none_of( R&& r, Pred pred, Proj proj = {} );
参数类型是泛型的,并具有以下约束
I
-std::input_iterator
S
-std::sentinel_for<I>
R
-ranges::input_range
Proj
- (无)Pred
:- (1) -
std::indirect_unary_predicate<std::projected<I, Proj>>
- (2) -
std::indirect_unary_predicate<std::projected<ranges::iterator_t<R>, Proj>>
- (1) -
对于所有重载,Proj
模板参数的默认类型为 std::identity
。
// (1)
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 = {} );
// (2)
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 = {} );
检查谓词对于范围中的所有元素是否都不成立。
-
(1) 检查一元谓词
pred
对于范围 [first
;last
) 中的所有元素(在通过投影proj
投影后)是否都不返回true
。 -
(2) 与 (1) 相同,但使用
r
作为源范围,如同使用ranges::begin(r)
作为first
和ranges::end(r)
作为last
。
本页描述的函数类实体是niebloids。
参数
first last | 要检查的元素范围。 |
r | 要检查的元素范围。 |
proj | 应用于元素的投影。 |
pred | 应用于投影元素的谓词。 |
返回值
如果对于范围中的每个迭代器 i
,std::invoke(pred, std::invoke(proj, *i)) == false
为 true
,否则为 false
。
如果范围为空,则返回 true
。
复杂度
最多 last - first
次谓词和投影应用。
异常
(无)
可能的实现
none_of(1) 和 all_of(2)
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 <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::none_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::none_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