Algorithm Find First Of Function - C++ Programming Tutorial
C++ Course / STL Algorithm / Algorithm Find First Of Function

Algorithm Find First Of Function

BLUF: Mastering Algorithm Find First Of Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Algorithm Find First Of Function

C++ is renowned for its efficiency. Learn how Algorithm Find First Of Function enables low-level control and high-performance computing in the tutorial below.

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

Example

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

Example

#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:

Output

Match 1 is: A
Match 1 is: a

Example 2

Example

#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:

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.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience