Home C++ Algorithms Functions all_of() C++ std::all_of() Function 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.
std::all_of(v.begin(), v.end(), pred);
std::all_of(v.begin() + 1, v.end(), pred); Call What 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 template<class InputIt, class UnaryPred>
bool all_of(InputIt first, InputIt last, UnaryPred p); Parameters Parameter Description first, last Required. The range to check. It starts at first and stops before last. p Required. A function that takes one element and returns true or false.
Return Value Return Value Description trueEvery element matched, or the range is empty. falseAt least one element did not match.
C++ Examples 1. All positive 2. One failure 3. Empty range Example 1 — All positive Check that every number in a vector is above zero.
#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 The lambda is the rule: a number passes when it is greater than 0. all_of() tests 3, 7, 1 and 9 in order. Each one passes.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.
#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 n % 2 == 0 is true for even numbers only.2 and 4 pass, but 5 is odd, so the test fails at the third element. 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.
#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;
} Empty result: true
Size: 0 How It Works The vector has zero elements, so the predicate is never called. With nothing to fail, all_of() returns true. Printing size() shows why: the answer is true only because the range is empty. Check for empty first when that matters. Frequently Asked Questions What does std::all_of() do? In C++, std::all_of() checks whether every element in a range matches a condition. Include <algorithm> to use it.
Which header and C++ version do I need for std::all_of()? Include <algorithm>. It needs C++11 or newer, so compile with -std=c++11 or later.
What does std::all_of() return? It returns true when every element matches the predicate (or the range is empty), and false otherwise.
Does all_of() return true for an empty range? Yes. An empty range has no element that fails the test, so std::all_of() returns true.
What is the difference between all_of(), any_of(), and none_of()? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion