std::experimental::ranges::all_of,std::experimental::ranges::any_of, (3) Linux Manual Page
std::experimental::ranges::all_of,std::experimental::ranges::any_of, – std::experimental::ranges::all_of,std::experimental::ranges::any_of,
Synopsis
Defined in header <experimental/ranges/algorithm>
template< InputIterator I, Sentinel<I> S, class Proj =
ranges::identity,
(1) (ranges TS)
IndirectUnaryPredicate<projected<I, Proj>> Pred >
bool all_of( I first, S last, Pred pred, Proj proj = Proj{} );
template< InputRange R, class Proj = ranges::identity,
IndirectUnaryPredicate<projected<ranges::iterator_t<R>, Proj>>
>
bool all_of( R&& r, Pred pred, Proj proj = Proj{} );
template< InputIterator I, Sentinel<I> S, class Proj =
ranges::identity,
(3) (ranges TS)
IndirectUnaryPredicate<projected<I, Proj>> Pred >
bool any_of( I first, S last, Pred pred, Proj proj = Proj{} );
template< InputRange R, class Proj = ranges::identity,
IndirectUnaryPredicate<projected<ranges::iterator_t<R>, Proj>>
>
bool any_of( R&& r, Pred pred, Proj proj = Proj{} );
template< InputIterator I, Sentinel<I> S, class Proj = identity,
IndirectUnaryPredicate<projected<I, Proj>>
bool none_of( I first, S last, Pred pred, Proj proj = Proj{} );
template< InputRange R, class Proj = ranges::identity,
IndirectUnaryPredicate<projected<ranges::iterator_t<R>, Proj>>
>
bool none_of( R&& r, Pred pred, Proj proj = Proj{} );
1)
last).
3)
[first, last).
5)
last).
2,4,6)
as first and ranges::end(r)
Notwithstanding the declarations depicted above, the actual number and order of
template parameters for algorithm declarations is unspecified. Thus, if explicit
template arguments are used when calling an algorithm, the program is probably
non-portable.
Parameters
first, last – the range of the elements to examine
r
pred
proj
Return value
1-2)
Returns true if the range is empty.
3-4)
otherwise. Returns false if the range is empty.
5-6)
Returns true if the range is empty.
Complexity
1-6)
applications of the projection.
Possible implementation
First version
template< InputIterator I, Sentinel<I> S, class Proj = ranges::identity,
return ranges::find_if_not(first, last, std::ref(pred), std::ref(proj))
template< InputRange R, class Proj = ranges::identity,
return ranges::all_of(ranges::begin(r), ranges::end(r), std::ref(pred), std::ref(proj)); }
Second version
template< InputIterator I, Sentinel<I> S, class Proj = ranges::identity,
return ranges::find_if(first, last, std::ref(pred), std::ref(proj))
template< InputRange R, class Proj = ranges::identity,
IndirectUnaryPredicate<projected<ranges::iterator_t<R>, Proj>>
return ranges::any_of(ranges::begin(r), ranges::end(r), std::ref(pred), std::ref(proj)); }
return ranges::find_if(first, last, std::ref(pred), std::ref(proj))
template< InputRange R, class Proj = ranges::identity,
return ranges::none_of(ranges::begin(r), ranges::end(r), std::ref(pred), std::ref(proj)); }
Example
// Run this code
#include <vector>
#include <numeric>
#include <experimental/ranges/algorithm>
#include <experimental/ranges/iterator>
#include <iterator>
#include <iostream>
#include <functional>
namespace ranges = std::experimental::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, 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 << "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";
}
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 << "At least one number is divisible by 7\n";
}
}
Output:
See also
all_of
any_of
none_of range
(C++11)
(C++11)
(C++11)
