std::all_of, std::any_of, std::none_of

From cppreference.com
< cpp‎ | algorithm
 
 
Algorithm library
Execution policies (C++17)
Non-modifying sequence operations
all_ofany_ofnone_of
(C++11)(C++11)(C++11)
(C++17)
Modifying sequence operations
(C++11)
(C++11)
(C++11)
(C++11)

Operations on uninitialized storage
Partitioning operations
Sorting operations
(C++11)
(C++11)
Binary search operations
Set operations (on sorted ranges)
Heap operations
(C++11)
(C++11)
Minimum/maximum operations
(C++11)
(C++11)
(C++17)

Permutations
(C++11)
Numeric operations
C library
 
Defined in header <algorithm>
template< class InputIt, class UnaryPredicate >
bool all_of( InputIt first, InputIt last, UnaryPredicate p );
(1) (since C++11)
template< class ExecutionPolicy, class InputIt, class UnaryPredicate >
bool all_of( ExecutionPolicy&& policy, InputIt first, InputIt last, UnaryPredicate p );
(2) (since C++17)
template< class InputIt, class UnaryPredicate >
bool any_of( InputIt first, InputIt last, UnaryPredicate p );
(3) (since C++11)
template< class ExecutionPolicy, class InputIt, class UnaryPredicate >
bool any_of( ExecutionPolicy&& policy, InputIt first, InputIt last, UnaryPredicate p );
(4) (since C++17)
template< class InputIt, class UnaryPredicate >
bool none_of( InputIt first, InputIt last, UnaryPredicate p );
(5) (since C++11)
template< class ExecutionPolicy, class InputIt, class UnaryPredicate >
bool none_of( ExecutionPolicy&& policy, InputIt first, InputIt last, UnaryPredicate p );
(6) (since C++17)
1) Checks if unary predicate p returns true for all elements in the range [first, last).
3) Checks if unary predicate p returns true for at least one element in the range [first, last).
5) Checks if unary predicate p returns true for no elements in the range [first, last).
2,4,6) Same as (1,3,5), but executed according to policy. These overloads do not participate in overload resolution unless std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> is true

Contents

[edit] Parameters

first, last - the range of elements to examine
policy - the execution policy to use. See execution policy for details.
p - unary predicate .

The signature of the predicate function should be equivalent to the following:

 bool pred(const Type &a);

The signature does not need to have const &, but the function must not modify the objects passed to it.
The type Type must be such that an object of type InputIt can be dereferenced and then implicitly converted to Type. ​

Type requirements
-
InputIt must meet the requirements of InputIterator.
-
UnaryPredicate must meet the requirements of Predicate.

[edit] Return value

1-2) true if unary predicate returns true for all elements in the range, false otherwise. Returns true if the range is empty.
3-4) true if unary predicate returns true for at least one element in the range, false otherwise. Returns false if the range is empty.
5-6) true if unary predicate returns true for no elements in the range, false otherwise. Returns true if the range is empty.

[edit] Complexity

At most last - first applications of the predicate

[edit] Exceptions

The overloads with a template parameter named ExecutionPolicy report errors as follows:

  • If execution of a function invoked as part of the algorithm throws an exception,
  • if policy is std::parallel_vector_execution_policy, std::terminate is called
  • if policy is std::sequential_execution_policy or std::parallel_execution_policy, the algorithm exits with an std::exception_list containing all uncaught exceptions. If there was only one uncaught exception, the algorithm may rethrow it without wrapping in std::exception_list. It is unspecified how much work the algorithm will perform before returning after the first exception was encountered.
  • if policy is some other type, the behavior is implementation-defined
  • If the algorithm fails to allocate memory (either for itself or to construct an std::exception_list when handling a user exception), std::bad_alloc is thrown.

[edit] Possible implementation

First version
template< class InputIt, class UnaryPredicate >
bool all_of(InputIt first, InputIt last, UnaryPredicate p)
{
    return std::find_if_not(first, last, p) == last;
}
Second version
template< class InputIt, class UnaryPredicate >
bool any_of(InputIt first, InputIt last, UnaryPredicate p)
{
    return std::find_if(first, last, p) != last;
}
Third version
template< class InputIt, class UnaryPredicate >
bool none_of(InputIt first, InputIt last, UnaryPredicate p)
{
    return std::find_if(first, last, p) == last;
}

[edit] Example

#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:

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

[edit] See also

parallelized version of std::all_of
(function template)
parallelized version of std::any_of
(function template)
parallelized version of std::none_of
(function template)