函式模板
<algorithm>

std::find_first_of

相等 (1)
template <class ForwardIterator1, class ForwardIterator2>   ForwardIterator1 find_first_of (ForwardIterator1 first1, ForwardIterator1 last1,                                   ForwardIterator2 first2, ForwardIterator2 last2);
謂詞 (2)
template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>   ForwardIterator1 find_first_of (ForwardIterator1 first1, ForwardIterator1 last1,                                   ForwardIterator2 first2, ForwardIterator2 last2,                                   BinaryPredicate pred);
相等 (1)
template <class InputIterator, class ForwardIterator>   InputIterator find_first_of (InputIterator first1, InputIterator last1,                                   ForwardIterator first2, ForwardIterator last2);
謂詞 (2)
template <class InputIterator, class ForwardIterator, class BinaryPredicate>   InputIterator find_first_of (InputIterator first1, InputIterator last1,                                   ForwardIterator first2, ForwardIterator last2,                                   BinaryPredicate pred);
從集合中查詢範圍內的元素
返回一個迭代器,指向範圍 [first1,last1) 中與 [first2,last2) 中的任意元素匹配的第一個元素。如果未找到此類元素,則函式返回 last1

範圍 [first1,last1) 中的元素將使用 operator==(或版本 (2) 中的 pred)依次與 [first2,last2) 中的每個值進行比較,直到找到一對匹配項。

此函式模板的行為等同於
1
2
3
4
5
6
7
8
9
10
11
12
13
template<class InputIterator, class ForwardIterator>
  InputIterator find_first_of ( InputIterator first1, InputIterator last1,
                                ForwardIterator first2, ForwardIterator last2)
{
  while (first1!=last1) {
    for (ForwardIterator it=first2; it!=last2; ++it) {
      if (*it==*first1)          // or: if (pred(*it,*first)) for version (2)
        return first1;
    }
    ++first1;
  }
  return last1;
}

引數

first1, last1
正向迭代器,指向被搜尋序列的起始和結束位置。使用的範圍是 [first1,last1),它包含 first1last1 之間的所有元素,包括 first1 指向的元素,但不包括 last1 指向的元素。
輸入迭代器,指向被搜尋序列的起始和結束位置。使用的範圍是 [first1,last1),它包含 first1last1 之間的所有元素,包括 first1 指向的元素,但不包括 last1 指向的元素。
first2, last2
正向迭代器,指向要搜尋的元素值的起始和結束位置。使用的範圍是 [first2,last2)
對於 (1),兩個範圍中的元素應為可使用 operator== 進行比較的型別(其中第一個範圍的元素為左側運算元,第二個範圍的元素為右側運算元)。
pred
二元函式,接受兩個元素作為引數(分別來自兩個序列,順序相同),並返回一個可轉換為 bool 的值。返回的值指示在當前函式上下文中,元素是否被認為匹配。
該函式不得修改其任何引數。
這可以是函式指標或函式物件。

返回值

一個迭代器,指向 [first1,last1) 中屬於 [first2,last2) 的第一個元素。
如果沒有找到匹配項,函式將返回 last1
如果 [first2,last2) 是一個空範圍,結果未定義。
如果 [first2,last2) 是一個空範圍,函式將返回 last1

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// find_first_of example
#include <iostream>     // std::cout
#include <algorithm>    // std::find_first_of
#include <vector>       // std::vector
#include <cctype>       // std::tolower

bool comp_case_insensitive (char c1, char c2) {
  return (std::tolower(c1)==std::tolower(c2));
}

int main () {
  int mychars[] = {'a','b','c','A','B','C'};
  std::vector<char> haystack (mychars,mychars+6);
  std::vector<char>::iterator it;

  int needle[] = {'A','B','C'};

  // using default comparison:
  it = find_first_of (haystack.begin(), haystack.end(), needle, needle+3);

  if (it!=haystack.end())
    std::cout << "The first match is: " << *it << '\n';

  // using predicate comparison:
  it = find_first_of (haystack.begin(), haystack.end(),
                      needle, needle+3, comp_case_insensitive);

  if (it!=haystack.end())
    std::cout << "The first match is: " << *it << '\n';

  return 0;
}

輸出
The first match is: A
The first match is: a


複雜度

最多線性於 count1*count2(其中 countXfirstXlastX 之間的距離):比較元素直到找到匹配項。

資料競爭

兩個範圍中的部分(或全部)物件將被訪問(對於 [first1,last1) 最多訪問一次,對於 [first2,last2) 可能訪問多次)。

異常

如果任何元素比較(或pred)丟擲異常,或者任何迭代器操作丟擲異常,則丟擲異常。
請注意,無效引數會導致未定義行為

另見