Defined in header <algorithm> | ||||
---|---|---|---|---|
| (1) | (since C++11) | ||
| (2) | (since C++11) | ||
| (3) | (since C++11) |
1) Checks if unary predicate
p
returns true
for all elements in the range [first, last)
. 2) Checks if unary predicate
p
returns true
for at least one element in the range [first, last)
. 3) Checks if unary predicate
p
returns true
for no elements in the range [first, last)
.Parameters
first, last | - | the range of elements to examine |
p | - | unary predicate . The signature of the predicate function should be equivalent to the following:
The signature does not need to have |
Type requirements | ||
- InputIt must meet the requirements of InputIterator . | ||
- UnaryPredicate must meet the requirements of Predicate . |
Return value
1)
true
if unary predicate returns true
for all elements in the range, false
otherwise. Returns true
if the range is empty. 2)
true
if unary predicate returns true
for at least one element in the range, false
otherwise. Returns false
if the range is empty. 3)
true
if unary predicate returns true
for no elements in the range, false
otherwise. Returns true
if the range is empty.Complexity
At most last
- first
applications of the predicate.
Possible implementation
First version | ||
---|---|---|
| ||
Second version | ||
| ||
Third version | ||
|
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #include <vector> #include <numeric> #include <algorithm> #include <iterator> #include <iostream> #include <functional> int main() { std::vector< int > v(10, 2); std::partial_sum(v.cbegin(), v.cend(), v.begin()); std::cout << "Among the numbers: " ; std::copy(v.cbegin(), v.cend(), std::ostream_iterator< int >(std::cout, " " )); std::cout << '\n' ; if (std::all_of(v.cbegin(), v.cend(), []( int i){ return i % 2 == 0; })) { std::cout << "All numbers are even\n" ; } if (std::none_of(v.cbegin(), v.cend(), 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 (std::any_of(v.cbegin(), v.cend(), DivisibleBy(7))) { std::cout << "At least one number is divisible by 7\n" ; } } |
Output:
1 2 3 4 | 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 |
See also
std::experimental::parallel::all_of
(parallelism TS) | parallelized version of std::all_of (function template) |
std::experimental::parallel::any_of
(parallelism TS) | parallelized version of std::any_of (function template) |
std::experimental::parallel::none_of
(parallelism TS) | parallelized version of std::none_of (function template) |
Please login to continue.