C++ std::find_if() Function

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

Definition

In C++, std::find_if() works like std::find(), but instead of a value you give it a predicate. It returns an iterator to the first element for which the predicate is true.

Use it when the rule is more than plain equality, for example "the first score above 90" or "the first student who passed".

Syntax

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

C++
std::find_if(v.begin(), v.end(), pred);
std::find_if(v.begin(), v.end(), [](int n) { return n > 10; });
CallWhat it does
std::find_if(v.begin(), v.end(), pred);Return the first element that passes the test.
std::find_if(v.begin(), v.end(), [](int n) { return n > 10; });Write the rule inline with a lambda.
Library declarations
C++
template<class InputIt, class UnaryPred>
InputIt find_if(InputIt first, InputIt last, UnaryPred p);

Parameters

ParameterDescription
first, lastRequired. The range to search.
pRequired. A predicate that returns true for the element you want.

Return Value

Return ValueDescription
IteratorPoints at the first element where p returned true.
lastReturned when no element passes the test.

C++ Examples

Example 1 — First above 10

Print the first number bigger than 10.

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

int main() {
    std::vector<int> v = {4, 8, 15, 16, 23, 42};
    auto it = std::find_if(v.begin(), v.end(), [](int n) { return n > 10; });

    if (it != v.end()) {
        std::cout << "First number above 10: " << *it << '\n';
    }
    return 0;
}

How It Works

  1. The lambda says a number matches when it is greater than 10.
  2. 4 and 8 fail. 15 passes, so the search ends at 15.
  3. *it reads the value that the iterator points at.

Example 2 — Nothing matches

Look for an even number in a list of odd numbers.

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

int main() {
    std::vector<int> v = {1, 3, 5};
    auto it = std::find_if(v.begin(), v.end(), [](int n) { return n % 2 == 0; });

    if (it == v.end()) {
        std::cout << "No even number\n";
    }
    return 0;
}

How It Works

  1. Every number in the list is odd, so the predicate is always false.
  2. When the loop runs out of elements, find_if() returns v.end().
  3. The it == v.end() check catches this case before any bad read.

Example 3 — Search structs

Find the first student whose score is at least 90.

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

struct Student {
    std::string name;
    int score;
};

int main() {
    std::vector<Student> list = {{"Asha", 72}, {"Ben", 91}, {"Chen", 85}};
    auto it = std::find_if(list.begin(), list.end(),
                           [](const Student& s) { return s.score >= 90; });

    if (it != list.end()) {
        std::cout << it->name << " scored " << it->score << '\n';
    }
    return 0;
}

How It Works

  1. The vector holds Student objects, so the lambda receives one Student at a time.
  2. Asha has 72, which fails. Ben has 91, which passes.
  3. it->name and it->score read the fields of the matching object.

Frequently Asked Questions

In C++, std::find_if() finds the first element that satisfies a condition. Include <algorithm> to use it.
Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
It returns an iterator to the first element for which the predicate is true, or the last iterator if there is none.
A predicate is a function or lambda that takes a value and returns true or false. find_if() calls it on each element.
find() compares each element with a value using ==. find_if() asks a predicate, so you can use any rule.

Next: find_if_not()

Finds the first element that does not satisfy a condition.

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

24 people found this page helpful