C++ std::any_of() Function

Definition
In C++, std::any_of() scans a range and returns true as soon as it finds one element that matches the condition.
It is a clean replacement for a hand-written loop with a found flag. An empty range always returns false.
Syntax
Add #include <algorithm>, then call std::any_of(). Start with these two lines.
std::any_of(v.begin(), v.end(), pred);
std::any_of(names.begin(), names.end(), pred);
| Call | What it does |
|---|
std::any_of(v.begin(), v.end(), pred); | Looks through the whole range. |
std::any_of(names.begin(), names.end(), pred); | Works the same on any container, such as a vector of strings. |
The search stops at the first match, so it can be faster than checking everything.
Library declarations
template<class InputIt, class UnaryPred>
bool any_of(InputIt first, InputIt last, UnaryPred p);
Parameters
| Parameter | Description |
|---|
| first, last | Required. The range to search. |
| p | Required. A predicate: takes one element and returns true or false. |
Return Value
| Return Value | Description |
|---|
true | At least one element matched. |
false | No element matched, or the range is empty. |
C++ Examples
Example 1 — Find an even number
Check whether a list has at least one even value.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 3, 5, 6};
bool found = std::any_of(v.begin(), v.end(), [](int n) { return n % 2 == 0; });
if (found) {
std::cout << "Found an even number\n";
}
return 0;
}
How It Works
- The lambda returns
true for even numbers. - 1, 3 and 5 fail, then 6 passes at the last position.
- One pass is enough, so
any_of() returns true and the message prints.
Example 2 — No long names
Run the test on a vector of strings.
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
int main() {
std::vector<std::string> names = {"anna", "bob", "cy"};
bool longName = std::any_of(names.begin(), names.end(),
[](const std::string& s) { return s.size() > 5; });
std::cout << (longName ? "Has a long name" : "No long names") << '\n';
return 0;
}
How It Works
- The lambda takes a
const std::string&, so no string is copied. - The longest name has 4 letters, so
size() > 5 is false for all three. - With no match anywhere,
any_of() returns false and the second message prints.
Example 3 — Use a captured limit
A lambda can read a variable from outside with a capture.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> scores = {40, 55, 38};
int limit = 50;
bool passed = std::any_of(scores.begin(), scores.end(),
[limit](int s) { return s > limit; });
if (passed) {
std::cout << "Someone passed the limit of " << limit << '\n';
}
return 0;
}
Someone passed the limit of 50
How It Works
[limit] copies the variable into the lambda so the rule can use it.- 40 is not above 50, but 55 is, so the search ends at the second score.
- Changing
limit changes the rule without rewriting the lambda.
Frequently Asked Questions
In C++, std::any_of() checks whether at least one 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 if at least one element matches the predicate, and false if none match or the range is empty.
Yes. It stops at the first element that matches, so it may not visit every element.
any_of() only returns true or false. Use std::find_if() when you need the iterator or index of the match.
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.
16 people found this page helpful