TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C++ algorithm find_first_of() function

C++ algorithm find_first_of() function tutorial for beginners and professionals with examples on adjacent_find(),any_of(), copy(), copy_if(), count(), count_if(), equal(), find(), find_end(), find_first_of(), find_if(), find_if_not(), for_each() etc.

<< Back to CPP

C++ Algorithm Function find_first_of()

C++ Algorithm find_first_of() function compares the values stored in two containers i.e [first1, last1) and [first2, last2). If an element similar to the one in the range [first2, last2) is found in [first1, last1) then an iterator to that element is returned by the function. In the situations where more than one similar element is present in both the ranges, the iterator to the first similar element is returned. If the case arises where no two elements are common in the range then an iterator to last1 element is returned.

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

first1: It is a forward iterator to the first element in the range [first1, last1) where the element itself is included in the range.

last1: It is a forward iterator to the last element in the range [first1, last1) where the element itself is not included in the range.

first2: It is a forward iterator to the first element in the range [first2, last2) where the element itself is included in the range.

last2: It is a forward iterator to the last element in the range [first2, last2) where the element itself is not included in the range.

pred: It is a binary function that accepts two elements as arguments and performs the task designed by the function.

Return value

The function returns an iterator to the first common element of the range [first1, last1) that is also a part of the range [first2,last2).In case no such element is found then the function returns 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 complexity of the function is specified by count1*count2. Here countX specifies the distance between the firstX and lastX. The comparison is done until a matching element is found.

Data races

From both the ranges some of the elements are accessed.

Exceptions

The function throws an exception if any of the argument throws one.






Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf