The C++ function findfirstof algorithm compares the values within two containers specified as [first1, last1) and [first2, last2). It searches for an element in the range [first2, last2) that matches any element in the range [first1, last1). When a matching element is found, the function returns an iterator pointing to that element. If multiple matching elements exist in both ranges, the iterator to the first occurrence is returned. In cases where there are no common elements between the ranges, the function returns an iterator pointing to the last element in the range [first1, last1).
Syntax
template<class ForwardIterator1, classForwardIterator2>
ForwardIterator1 find_first_of(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2);
template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
ForwardIterator1 find_first_of(ForwardIterator1 first1,ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
Parameter
It represents a forward iterator pointing to the initial element within the range [first1, last1), inclusive of the element itself.
It serves as a forward iterator pointing to the final element within the range [first1, last1) while excluding the element itself from the range.
It represents a forward iterator pointing to the initial element within the range [first2, last2) with the element itself being part of the range.
It represents a forward iterator pointing to the element just before the last one within the range [first2, last2), excluding the last element itself.
pred : It is a binary operation that takes two elements as parameters and executes the specified task defined within the operation.
Return value
The function provides an iterator pointing to the initial common element within the [first1, last1) range, which is also present in the [first2, last2) range. If no such element exists, the function will return the last1 element.
Example 1
#include <iostream>
#include <algorithm>
#include <vector>
#include <cctype>
bool case_insensitive (char a1, char a2)
{
return (std::tolower(a1)==std::tolower(a2));
}
int main ()
{
int newchars[] = {'a','b','c','A','B','C'};
std::vector<char> haystack (newchars,newchars+6);
std::vector<char>::iterator ti;
int patt[] = {'A','B','C'};
ti = find_first_of (haystack.begin(), haystack.end(), patt, patt+3);
if (ti!=haystack.end())
std::cout << "Match 1 is: " << *ti << '\n';
ti = find_first_of (haystack.begin(), haystack.end(),
patt, patt+3, case_insensitive);
if (ti!=haystack.end())
std::cout << "Match 1 is: " << *ti << '\n';
return 0;
}
Output:
Match 1 is: A
Match 1 is: a
Example 2
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
string str1 = "We are trying to get an idea of the find_first_of function in C++";
string str2= {'a','A','e','E','i','I','o','O','u','U'};
auto pi = std::find_first_of(str1.begin(), str1.end(), str2.begin(), str2.end());
cout<<"First vowel has been discovered at index "<<(pi-str1.begin())<<"\n";
return 0;
}
Output:
First vowel has been discovered at index 1
Complexity
The intricacy of the function is determined by count1 multiplied by count2. Each countX represents the separation between the initial X and the final X. The evaluation continues until a corresponding element is located.
Data races
From both the ranges, certain elements are retrieved.
Exceptions
The function will raise an exception if any of its arguments also raises an exception.