| Defined in header <experimental/ranges/algorithm>
|
||
| 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{} ); |
(1) | (ranges TS) |
| 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{} ); |
(2) | (ranges TS) |
| 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{} ); |
(3) | (ranges TS) |
| 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{} ); |
(4) | (ranges TS) |
| 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{} ); |
(5) | (ranges TS) |
| 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{} ); |
(6) | (ranges TS) |
pred returns true for all elements in the range [first, last).pred returns true for at least one element in the range [first, last).pred returns true for no elements in the range [first, last).r as the source range, as if using ranges::begin(r) as first and ranges::end(r) as last.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 | - | the range of the elements to examine |
| pred | - | predicate to apply to the projected elements |
| proj | - | projection to apply to the elements |
Return value
true if pred returns true for all elements in the range, false otherwise. Returns true if the range is empty.true if pred returns true for at least one element in the range, false otherwise. Returns false if the range is empty.true if pred returns true for no elements in the range, false otherwise. Returns true if the range is empty.Complexity
last - first applications of the predicate and last - first applications of the projection.Possible implementation
| First version |
|---|
|
| Second version |
|
| Third version |
|
Example
#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 << "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";
}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 7See also
| (C++11)(C++11)(C++11) |
checks if a predicate is true for all, any or none of the elements in a range (function template) |
Content from this page on cppreference.com, licensed under CC-BY-SA 3.0 and GFDL. Modified: restyled, site navigation removed, code windows added. Offline snapshot of 9 February 2025.