Home C++ Algorithms Functions find_if() C++ std::find_if() Function 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.
std::find_if(v.begin(), v.end(), pred);
std::find_if(v.begin(), v.end(), [](int n) { return n > 10; }); Call What 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 template<class InputIt, class UnaryPred>
InputIt find_if(InputIt first, InputIt last, UnaryPred p); Parameters Parameter Description first, last Required. The range to search. p Required. A predicate that returns true for the element you want.
Return Value Return Value Description Iterator Points at the first element where p returned true. lastReturned when no element passes the test.
C++ Examples 1. First above 10 2. Nothing matches 3. Search structs Example 1 — First above 10 Print the first number bigger than 10.
#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;
} First number above 10: 15 How It Works The lambda says a number matches when it is greater than 10. 4 and 8 fail. 15 passes, so the search ends at 15. *it reads the value that the iterator points at.Example 2 — Nothing matches Look for an even number in a list of odd numbers.
#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 Every number in the list is odd, so the predicate is always false. When the loop runs out of elements, find_if() returns v.end(). 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.
#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 The vector holds Student objects, so the lambda receives one Student at a time. Asha has 72, which fails. Ben has 91, which passes. it->name and it->score read the fields of the matching object.Frequently Asked Questions What does std::find_if() do? In C++, std::find_if() finds the first element that satisfies a condition. Include <algorithm> to use it.
Which header and C++ version do I need for std::find_if()? Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
What does std::find_if() return? It returns an iterator to the first element for which the predicate is true, or the last iterator if there is none.
What is a predicate in C++? A predicate is a function or lambda that takes a value and returns true or false. find_if() calls it on each element.
What is the difference between find() and find_if()? find() compares each element with a value using ==. find_if() asks a predicate, so you can use any rule.
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.
24 people found this page helpful
Helpful Share Copy link Suggestion