Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

std::none_of — no element satisfies a condition

std::none_of from <algorithm> checks whether no element in a range satisfies a predicate. It returns bool: true means there are no matches, while false means at least one match exists. An empty range produces true.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

#include <algorithm>
std::none_of(first, last, pred)

std::none_of(first, last, pred) examines the half-open range [first, last): it includes first but excludes last. It returns false if pred(*i) is true for at least one element; otherwise it returns true. An empty range produces true because it contains no element satisfying the condition.

Include <algorithm> and supply a valid iterator range and a predicate whose result can be tested as a Boolean. The ordinary overload requires input iterators. The algorithm does not change the elements; the predicate must not modify them through its argument either. The result is a bool, not an iterator or a match count.

For N elements, the algorithm applies the predicate at most N times. Do not assume that the predicate will run for every element. An execution-policy overload also exists and requires forward iterators; the example uses the overload without a policy.

In the example, is_negative tests x < 0. The first call returns true because none of the numbers is negative. The second also returns true because values.begin(), values.begin() defines an empty range. The supplied sources are an evolving standard draft; this entry stays within the interface available in C++23.

Examples

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

int main() {
    const std::array<int, 4> values{2, 4, 6, 8};
    const auto is_negative = [](int x) { return x < 0; };

    std::cout << std::boolalpha
              << std::none_of(values.begin(), values.end(), is_negative) << '\n'
              << std::none_of(values.begin(), values.begin(), is_negative) << '\n';
}

Result

true
true

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources