C++ std::any_of() Function

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

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.

C++
std::any_of(v.begin(), v.end(), pred);
std::any_of(names.begin(), names.end(), pred);
CallWhat 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
C++
template<class InputIt, class UnaryPred>
bool any_of(InputIt first, InputIt last, UnaryPred p);

Parameters

ParameterDescription
first, lastRequired. The range to search.
pRequired. A predicate: takes one element and returns true or false.

Return Value

Return ValueDescription
trueAt least one element matched.
falseNo 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.

C++
#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

  1. The lambda returns true for even numbers.
  2. 1, 3 and 5 fail, then 6 passes at the last position.
  3. 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.

C++
#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

  1. The lambda takes a const std::string&, so no string is copied.
  2. The longest name has 4 letters, so size() > 5 is false for all three.
  3. 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.

C++
#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;
}

How It Works

  1. [limit] copies the variable into the lambda so the rule can use it.
  2. 40 is not above 50, but 55 is, so the search ends at the second score.
  3. 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.

Next: binary_search()

Checks whether a value exists in a sorted range.

binary_search() 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.

16 people found this page helpful