std::experimental::ranges:: all_of, std::experimental::ranges:: any_of, std::experimental::ranges:: none_of
|
template
<
InputIterator I, Sentinel
<
I
>
S,
class
Proj
=
ranges::
identity
,
IndirectUnaryPredicate
<
projected
<
I, Proj
>>
Pred
>
|
(1) | (ranges TS) |
|
template
<
InputRange R,
class
Proj
=
ranges::
identity
,
IndirectUnaryPredicate
<
projected
<
ranges::
iterator_t
<
R
>
, Proj
>>
Pred
>
|
(2) | (ranges TS) |
|
template
<
InputIterator I, Sentinel
<
I
>
S,
class
Proj
=
ranges::
identity
,
IndirectUnaryPredicate
<
projected
<
I, Proj
>>
Pred
>
|
(3) | (ranges TS) |
|
template
<
InputRange R,
class
Proj
=
ranges::
identity
,
IndirectUnaryPredicate
<
projected
<
ranges::
iterator_t
<
R
>
, Proj
>>
Pred
>
|
(4) | (ranges TS) |
|
template
<
InputIterator I, Sentinel
<
I
>
S,
class
Proj
=
identity,
IndirectUnaryPredicate
<
projected
<
I, Proj
>>
Pred
>
|
(5) | (ranges TS) |
|
template
<
InputRange R,
class
Proj
=
ranges::
identity
,
IndirectUnaryPredicate
<
projected
<
ranges::
iterator_t
<
R
>
, Proj
>>
Pred
>
|
(6) | (ranges TS) |
[
first
,
last
)
中的所有元素返回
true
。
[
first
,
last
)
中的至少一个元素返回
true
。
[
first
,
last
)
中的所有元素均返回
true
。
尽管存在上述声明,算法声明的实际模板参数数量和顺序是未指定的。因此,若在调用算法时使用了显式模板参数,该程序很可能不具备可移植性。
目录 |
参数
| first, last | - | 要检查的元素范围 |
| r | - | 要检查的元素范围 |
| pred | - | 应用于投影元素的谓词 |
| proj | - | 应用于元素的投影 |
返回值
复杂度
可能实现
| 第一版本 |
|---|
template<InputIterator I, Sentinel<I> S, class Proj = ranges::identity, IndirectUnaryPredicate<projected<I, Proj>> Pred> bool all_of(I first, S last, Pred pred, Proj proj = Proj{}) { return ranges::find_if_not(first, last, std::ref(pred), std::ref(proj)) == last; } template<InputRange R, class Proj = ranges::identity, IndirectUnaryPredicate<projected<ranges::iterator_t<R>, Proj>> Pred> bool all_of(R&& r, Pred pred, Proj proj = Proj{}) { return ranges::all_of(ranges::begin(r), ranges::end(r), std::ref(pred), std::ref(proj)); } |
| 第二版本 |
template<InputIterator I, Sentinel<I> S, class Proj = ranges::identity, IndirectUnaryPredicate<projected<I, Proj>> Pred> bool any_of(I first, S last, Pred pred, Proj proj = Proj{}) { return ranges::find_if(first, last, std::ref(pred), std::ref(proj)) != last; } template<InputRange R, class Proj = ranges::identity, IndirectUnaryPredicate<projected<ranges::iterator_t<R>, Proj>> Pred> bool any_of(R&& r, Pred pred, Proj proj = Proj{}) { return ranges::any_of(ranges::begin(r), ranges::end(r), std::ref(pred), std::ref(proj)); } |
| 第三版本 |
template<InputIterator I, Sentinel<I> S, class Proj = identity, IndirectUnaryPredicate<projected<I, Proj>> Pred> bool none_of(I first, S last, Pred pred, Proj proj = Proj{}) { return ranges::find_if(first, last, std::ref(pred), std::ref(proj)) == last; } template<InputRange R, class Proj = ranges::identity, IndirectUnaryPredicate<projected<ranges::iterator_t<R>, Proj>> Pred> bool none_of(R&& r, Pred pred, Proj proj = Proj{}) { return ranges::none_of(ranges::begin(r), ranges::end(r), std::ref(pred), std::ref(proj)); } |
示例
#include <experimental/ranges/algorithm> #include <experimental/ranges/iterator> #include <functional> #include <iostream> #include <iterator> #include <numeric> #include <vector> namespace ranges = std::experimental::ranges; int main() { std::vector<int> v(10, 2); std::partial_sum(v.cbegin(), v.cend(), v.begin()); std::cout << "在数字中: "; ranges::copy(v, ranges::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"; struct DivisibleBy { const int d; DivisibleBy(int n) : d(n) {} bool operator()(int n) const { return n % d == 0; } }; if (ranges::any_of(v, DivisibleBy(7))) std::cout << "至少有一个数字能被7整除\n"; }
输出:
在数字中: 2 4 6 8 10 12 14 16 18 20 所有数字都是偶数 没有数字是奇数 至少有一个数字能被7整除
参见
|
(C++11)
(C++11)
(C++11)
|
检查谓词是否对范围中的所有、任一或没有元素返回
true
(函数模板) |