std::find_first_of is used to compare elements between two containers. It compares all the elements in a range [first1,last1) with the elements in the range [first2,last2), and if any of the elements present in the second range is found in the first one , then it returns an iterator to that element.
If there are more than one element common in both the ranges, then an iterator to the first common element present in the first container is returned. In case there is no match, then iterator pointing to last1 is returned.
It can be used in two ways as shown below:
-
Comparing elements using ==:
Syntax:
Template ForwardIterator1 find_first_of(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2); first1: Forward iterator to the first element in the first range. last1: Forward iterator to the last element in the first range. first2: Forward iterator to the first element in the second range. last2: Forward iterator to the last element in the second range. Return Value: It returns an iterator to the first element in [first1,last1) that is part of [first2,last2). If no matches are found or [first2, last2) is empty, the function returns last1.
// C++ program to demonstrate
// the use of std::find_first_of
#include<iostream>
#include<vector>
#include<algorithm>
using
namespace
std;
int
main()
{
// Defining first container
vector<
int
>v = {1, 3, 3, 3, 10, 1, 3, 3, 7, 7, 8} , i;
// Defining second container
vector<
int
>v1 = {1, 3, 10};
vector<
int
>::iterator ip;
// Using std::find_first_of
ip = std::find_first_of(v.begin(), v.end(), v1.begin(),
v1.end());
// Displaying the first common element found
cout << *ip <<
"\n"
;
// Finding the second common element
ip = std::find_first_of(ip + 1, v.end(), v1.begin(),
v1.end());
// Displaying the second common element found
cout << *ip <<
"\n"
;
return
0;
}
Output:
1 3
Here, in both the vectors, the first common element is 1 and to find the second common element, we have passed the beginning of the first range as the element next to the first common element already found.
-
By comparing using a pre-defined function:
Syntax:
Template ForwardIterator1 find_first_of (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred); Here, first1, last1, first2 and last2 are the same as previous case. Pred: Binary function that accepts two elements as arguments (one of each of the two sequences, in the same order), and returns a value convertible to bool. The value returned indicates whether the elements are considered to match in the context of this function. The function shall not modify any of its arguments. This can either be a function pointer or a function object. Return Value: It returns an iterator to the first element in [first1,last1) that is part of [first2,last2). If no matches are found or [first2,last2) is empty, the function returns last1.
// C++ program to demonstrate
// the use of std::find_first_of
#include<iostream>
#include<vector>
#include<algorithm>
using
namespace
std;
// Defining the BinaryFunction
bool
Pred (
int
a,
int
b)
{
if
( a % b == 0) {
return
1;
}
else
{
return
0;
}
}
int
main()
{
// Defining first container
vector<
int
>v = {1, 5, 7, 11, 13, 15, 30, 30, 7} , i;
// Defining second container
vector<
int
>v1 = {2, 3, 4};
vector<
int
>::iterator ip;
// Using std::find_first_of
ip = std::find_first_of(v.begin(), v.end(), v1.begin(),
v1.end(), Pred);
// Displaying the first element satisfying Pred()
cout << *ip <<
"\n"
;
return
0;
}
Output:
15
Here, we have manipulated the binary function in such a way that we are trying to find the first number in the first container which is a multiple of any of the number in the second container. And in this case, 15 comes out to be the first one, as it is divisible by 3.
Possible Application: std::find_first_of can be used to find the first occurrence of any of the elements present in another container.
- One possible application of this is to find the first vowel in a sentence.
// C++ program to demonstrate the use of std::find_first_of
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using
namespace
std;
int
main()
{
// Defining first container
string s1 =
"You are reading about std::find_first_of"
;
// Defining second container containing list of vowels
string s2 = {
'a'
,
'A'
,
'e'
,
'E'
,
'i'
,
'I'
,
'o'
,
'O'
,
'u'
,
'U'
};
// Using std::find_first_of to find first occurrence of
// a vowel
auto
ip = std::find_first_of(s1.begin(),s1.end(),
s2.begin(), s2.end());
// Displaying the first vowel found
cout <<
"First vowel found at index "
<< (ip - s1.begin())
<<
"\n"
;
return
0;
}
Output:
First vowel found at index 1
Explanation: std::find_first_of searches for the first occurrence of any of the elements from the second container in the first one, and in this way , first vowel present in the sentence was found out.
- It can also be used to find the first odd and even numbers present in list.
// C++ program to find the first occurrence of an odd
// and even number
#include<iostream>
#include<vector>
#include<algorithm>
using
namespace
std;
// Defining the Predicate Function to find first occurrence
// of an odd number
bool
pred(
int
a,
int
b)
{
if
(a % b != 0) {
return
1;
}
else
{
return
0;
}
}
// Defining the Predicate Function to find first occurrence
// of an even number
bool
pred1(
int
a,
int
b)
{
if
(a % b == 0) {
return
1;
}
else
{
return
0;
}
}
int
main()
{
// Defining a vector
vector<
int
>v1 = {1, 3, 4, 5, 6, 7, 8, 10};
// Declaring a sub-sequence
vector<
int
>v2 = {2};
// Using std::find_first_of to find the first
// occurrence of an odd number
vector<
int
>::iterator ip;
ip = std::find_first_of(v1.begin(), v1.end(), v2.begin(),
v2.end(), pred);
// Displaying the index where the first odd number
// occurred
cout <<
"First odd no. occurs at index "
<< (ip - v1.begin());
// Using std::find_first_of to find the last occurrence
// of an even number
ip = std::find_first_of(v1.begin(), v1.end(), v2.begin(),
v2.end(), pred1);
// Displaying the index where the first even number
// occurred
cout <<
"\nFirst even no. occurs at index "
<< (ip - v1.begin());
return
0;
}
Output:
First odd no. occurs at index 0 First even no. occurs at index 2
Explanation: Here, we have stored 2 in one container and with the help of predicate function, we are looking for the first number in the first container which is not divisible by 2 (for odd number) and which is divisible by 2 (for even number).
Time Complexity: O(n1 * n2), where, n1 is the number of elements in the first range and n2 is the number of elements in the second range.