C++ std::find_first_of() Function

Definition
In C++, std::find_first_of() looks through one range and stops at the first element that is equal to any element of a second range.
It is handy for questions like "where is the first vowel?" or "which of these allowed values showed up first?".
Syntax
Add #include <algorithm>, then call std::find_first_of(). Start with these two lines.
std::find_first_of(a.begin(), a.end(), t.begin(), t.end());
std::find_first_of(a.begin(), a.end(), t.begin(), t.end(), cmp);
| Call | What it does |
|---|
std::find_first_of(a.begin(), a.end(), t.begin(), t.end()); | Return the first element of a that appears in t. |
std::find_first_of(a.begin(), a.end(), t.begin(), t.end(), cmp); | Use your own rule to compare two elements. |
Library declarations
template<class InputIt, class ForwardIt>
InputIt find_first_of(InputIt first, InputIt last,
ForwardIt s_first, ForwardIt s_last);
Parameters
| Parameter | Description |
|---|
| first, last | Required. The range to search. |
| s_first, s_last | Required. The set of values you are looking for. |
| pred | Optional. A function that takes one element from each range and says if they match. |
Return Value
| Return Value | Description |
|---|
| Iterator | Points at the first element in the main range that matches something in the second range. |
last | Returned when nothing matches. |
C++ Examples
Example 1 — Numbers from a list
Find the first number that is either 4 or 9.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {7, 3, 9, 4, 1};
std::vector<int> targets = {4, 9};
auto it = std::find_first_of(v.begin(), v.end(), targets.begin(), targets.end());
if (it != v.end()) {
std::cout << "First match is " << *it << " at index " << (it - v.begin()) << '\n';
}
return 0;
}
First match is 9 at index 2
How It Works
- Both 4 and 9 are targets, but 9 comes earlier in
v. - 7 and 3 do not match, so the search reaches 9 at index 2.
- The result follows the main range, so 4 is never reached.
Example 2 — First vowel
Find the first vowel in a word.
#include <algorithm>
#include <iostream>
#include <string>
int main() {
std::string word = "school";
std::string vowels = "aeiou";
auto it = std::find_first_of(word.begin(), word.end(), vowels.begin(), vowels.end());
std::cout << "First vowel: " << *it << " at index " << (it - word.begin()) << '\n';
return 0;
}
First vowel: o at index 2
How It Works
- The second range is a string of vowels, so any of those letters can match.
s and c and h are not vowels. The first o is at index 2.- We skip the end check here because we know the word has vowels. Check it for real input.
Example 3 — Custom comparison
Match numbers by their last digit.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {12, 25, 37};
std::vector<int> digits = {5, 7};
auto it = std::find_first_of(v.begin(), v.end(), digits.begin(), digits.end(),
[](int a, int d) { return a % 10 == d; });
std::cout << "First number ending in 5 or 7: " << *it << '\n';
return 0;
}
First number ending in 5 or 7: 25
How It Works
- The lambda gets
a from the main range and d from the second range. a % 10 is the last digit. 12 ends in 2, so it does not match.- 25 ends in 5, which is in the digit list, so the search stops at 25.
Frequently Asked Questions
In C++, std::find_first_of() finds the first element that matches any value from a second range. 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 of the main range that equals any element of the second range, or the last iterator.
find() looks for one value. find_first_of() looks for any value from a list.
No. The result is always the first matching element in the main range.
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.
11 people found this page helpful