Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

std::all_of: whether every element satisfies a condition

std::all_of returns true when a predicate is true for every element of a range. Its result is also true for an empty range.

Version: C++23

Syntax

template<class InputIt, class UnaryPred>
constexpr bool all_of(InputIt first, InputIt last, UnaryPred predicate);

template<class ExecutionPolicy, class ForwardIt, class UnaryPred>
bool all_of(ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, UnaryPred predicate);

std::all_of from <algorithm> tests [first, last) with a unary predicate. It returns false after finding an element that fails, and true when none fails. An empty range therefore returns true by vacuous truth.

The algorithm validates a whole collection, such as checking that every number is even. Without an execution policy it may stop after the first false; predicate calls never exceed the range length.

The predicate must not modify its element or rely on unsafe side effects. Execution-policy overloads may evaluate calls in a different order or concurrently. With any standard execution policy, including std::execution::seq, an uncaught exception from the predicate causes std::terminate.

Examples

#include <algorithm>
#include <array>
#include <iostream>

int main() {
    const std::array values{2, 4, 6};
    const bool allEven = std::all_of(values.begin(), values.end(), [](int value) {
        return value % 2 == 0;
    });
    std::cout << allEven << '\n';
}

Result

1

std::all_of → 1

Sources