function template
<algorithm>

std::search

相等 (1)
template <class ForwardIterator1, class ForwardIterator2>   ForwardIterator1 search (ForwardIterator1 first1, ForwardIterator1 last1,                            ForwardIterator2 first2, ForwardIterator2 last2);
predicate (2)
template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>   ForwardIterator1 search (ForwardIterator1 first1, ForwardIterator1 last1,                            ForwardIterator2 first2, ForwardIterator2 last2,                            BinaryPredicate pred);
在範圍內搜尋子序列
在範圍 [first1,last1) 中搜索由 [first2,last2) 定義的序列的第一次出現,並返回指向其第一個元素的迭代器,如果沒有找到則返回 last1

兩個範圍中的元素透過 operator==(或版本(2)中的 pred)進行順序比較:當 [first2,last2) 的所有元素與 [first1,last1) 中的相應元素都匹配時,才算找到了一個子序列。

此函式返回第一次出現匹配的迭代器。要獲取返回最後一次匹配的演算法,請參閱 find_end

此函式模板的行為等同於
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
template<class ForwardIterator1, class ForwardIterator2>
  ForwardIterator1 search ( ForwardIterator1 first1, ForwardIterator1 last1,
                            ForwardIterator2 first2, ForwardIterator2 last2)
{
  if (first2==last2) return first1;  // specified in C++11
  
  while (first1!=last1)
  {
    ForwardIterator1 it1 = first1;
    ForwardIterator2 it2 = first2;
    while (*it1==*it2) {    // or: while (pred(*it1,*it2)) for version 2
        ++it1; ++it2;
        if (it2==last2) return first1;
        if (it1==last1) return last1;
    }
    ++first1;
  }
  return last1;
}

引數

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

返回值

指向 [first1,last1)[first2,last2) 第一次出現的第一個元素的迭代器。
如果未找到序列,則函式返回 last1
如果 [first2,last2) 是一個空範圍,結果未定義。
如果 [first2,last2) 是一個空範圍,則函式返回 first1

示例

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
33
34
35
36
// search algorithm example
#include <iostream>     // std::cout
#include <algorithm>    // std::search
#include <vector>       // std::vector

bool mypredicate (int i, int j) {
  return (i==j);
}

int main () {
  std::vector<int> haystack;

  // set some values:        haystack: 10 20 30 40 50 60 70 80 90
  for (int i=1; i<10; i++) haystack.push_back(i*10);

  // using default comparison:
  int needle1[] = {40,50,60,70};
  std::vector<int>::iterator it;
  it = std::search (haystack.begin(), haystack.end(), needle1, needle1+4);

  if (it!=haystack.end())
    std::cout << "needle1 found at position " << (it-haystack.begin()) << '\n';
  else
    std::cout << "needle1 not found\n";

  // using predicate comparison:
  int needle2[] = {20,30,50};
  it = std::search (haystack.begin(), haystack.end(), needle2, needle2+3, mypredicate);

  if (it!=haystack.end())
    std::cout << "needle2 found at position " << (it-haystack.begin()) << '\n';
  else
    std::cout << "needle2 not found\n";

  return 0;
}

輸出
needle1 found at position 3
needle2 not found


複雜度

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

資料競爭

兩個範圍中的部分(或全部)物件都可能被訪問(可能不止一次)。

異常

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

另見