C++ std::all_of() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<algorithm> · C++11

Definition

In C++, std::all_of() looks at every element in a range and asks one question: does it match the condition? It returns true only when all of them do.

The condition is a small function called a predicate. It takes one element and returns true or false. An empty range returns true, because no element breaks the rule.

Syntax

Add #include <algorithm>, then call std::all_of(). Start with these two lines.

C++
std::all_of(v.begin(), v.end(), pred);
std::all_of(v.begin() + 1, v.end(), pred);
CallWhat it does
std::all_of(v.begin(), v.end(), pred);Tests the whole vector with a predicate.
std::all_of(v.begin() + 1, v.end(), pred);Tests only part of the range, here everything after the first element.

A lambda such as [](int n) { return n > 0; } is the easiest predicate to write.

Library declarations
C++
template<class InputIt, class UnaryPred>
bool all_of(InputIt first, InputIt last, UnaryPred p);

Parameters

ParameterDescription
first, lastRequired. The range to check. It starts at first and stops before last.
pRequired. A function that takes one element and returns true or false.

Return Value

Return ValueDescription
trueEvery element matched, or the range is empty.
falseAt least one element did not match.

C++ Examples

Example 1 — All positive

Check that every number in a vector is above zero.

C++
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {3, 7, 1, 9};
    bool ok = std::all_of(v.begin(), v.end(), [](int n) { return n > 0; });
    std::cout << (ok ? "All positive" : "Not all positive") << '\n';
    return 0;
}

How It Works

  1. The lambda is the rule: a number passes when it is greater than 0.
  2. all_of() tests 3, 7, 1 and 9 in order. Each one passes.
  3. No failure was found, so the result is true and the first message prints.

Example 2 — One failure

See what happens when a single element breaks the rule.

C++
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {2, 4, 5, 8};
    bool ok = std::all_of(v.begin(), v.end(), [](int n) { return n % 2 == 0; });
    std::cout << std::boolalpha << "All even? " << ok << '\n';
    return 0;
}

How It Works

  1. n % 2 == 0 is true for even numbers only.
  2. 2 and 4 pass, but 5 is odd, so the test fails at the third element.
  3. all_of() stops there. It does not look at 8, and the result is false.

Example 3 — Empty range

An empty vector has nothing that can break the rule.

C++
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> empty;
    bool r = std::all_of(empty.begin(), empty.end(), [](int n) { return n < 0; });
    std::cout << std::boolalpha << "Empty result: " << r << '\n';
    std::cout << "Size: " << empty.size() << '\n';
    return 0;
}

How It Works

  1. The vector has zero elements, so the predicate is never called.
  2. With nothing to fail, all_of() returns true.
  3. Printing size() shows why: the answer is true only because the range is empty. Check for empty first when that matters.

Frequently Asked Questions

In C++, std::all_of() checks whether every element in a range matches a condition. Include <algorithm> to use it.
Include <algorithm>. It needs C++11 or newer, so compile with -std=c++11 or later.
It returns true when every element matches the predicate (or the range is empty), and false otherwise.
Yes. An empty range has no element that fails the test, so std::all_of() returns true.
all_of() needs every element to match. any_of() needs at least one match. none_of() needs zero matches.

Next: any_of()

Checks whether at least one element in a range matches a condition.

any_of() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful