C++ std::find_first_of() Function

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

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.

C++
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);
CallWhat 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
C++
template<class InputIt, class ForwardIt>
InputIt find_first_of(InputIt first, InputIt last,
                      ForwardIt s_first, ForwardIt s_last);

Parameters

ParameterDescription
first, lastRequired. The range to search.
s_first, s_lastRequired. The set of values you are looking for.
predOptional. A function that takes one element from each range and says if they match.

Return Value

Return ValueDescription
IteratorPoints at the first element in the main range that matches something in the second range.
lastReturned when nothing matches.

C++ Examples

Example 1 — Numbers from a list

Find the first number that is either 4 or 9.

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

How It Works

  1. Both 4 and 9 are targets, but 9 comes earlier in v.
  2. 7 and 3 do not match, so the search reaches 9 at index 2.
  3. The result follows the main range, so 4 is never reached.

Example 2 — First vowel

Find the first vowel in a word.

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

How It Works

  1. The second range is a string of vowels, so any of those letters can match.
  2. s and c and h are not vowels. The first o is at index 2.
  3. 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.

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

How It Works

  1. The lambda gets a from the main range and d from the second range.
  2. a % 10 is the last digit. 12 ends in 2, so it does not match.
  3. 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.

Next: find_if()

Finds the first element that satisfies a condition.

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

11 people found this page helpful