C++ find_first_of()
 
 
 C++ 算法函数find_first_of()
 
  C++ 算法find_first_of()函数比较存储在两个容器中的值,即[first1,last1)和[first2,last2)。如果在[first1,last1)中发现与[first2,last2)范围内的元素相似的元素,则该函数将返回对该元素的迭代器。在两个范围中都存在多个相似元素的情况下,将返回第一个相似元素的迭代器。如果出现该范围内没有两个公共元素的情况,则返回last1元素的迭代器。
 
语法
 
 
 
  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);
 
   
  
参数
 
  first1 : 它是范围[first1,last1)中第一个元素的正向迭代器,其中元素本身包含在其中
 
  last1 : 它是范围[first1,last1)中last元素的正向迭代器,其中元素本身不包含在范围中。 
 
  first2 : 它是范围[first2,last2)中第一个元素的正向迭代器,其中元素本身包含在范围中。 
 
  last2 : 它是范围[first2,last2)中last元素的正向迭代器,其中元素本身不包含在范围中。 
 
  pred : 它是一个二进制函数,它接受两个元素作为参数并执行该函数设计的任务。 
 
返回值
 
 该函数将迭代器返回到范围[first1,last1)的第一个公共元素,该元素也是范围[first2,如果找不到这样的元素,则函数返回last1元素。
 
示例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;
} 
   
  
  输出: 
 
 
 
  Match 1 is: A
Match 1 is: a
 
   
  
示例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;
} 
   
  
  输出: 
 
 
 
  First vowel has been discovered at index 1
 
   
  
复杂度
 
 该功能的复杂度由count1 * count2指定。这里countX指定firstX和lastX之间的距离。比较会一直进行到找到匹配的元素为止。
 
数据竞争
 
 从这两个范围中访问某些元素。 
 
异常
 
 如果任何参数抛出一个异常,该函数将引发异常。