C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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. Syntaxtemplate<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); Parameterfirst1: 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 valueThe 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 ComplexityThe 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 racesFrom both the ranges some of the elements are accessed. ExceptionsThe function throws an exception if any of the argument throws one.
Next TopicC++ Algorithm find_if Function
|