function template
<algorithm>

std::find_if_not

template <class InputIterator, class UnaryPredicate>   InputIterator find_if_not (InputIterator first, InputIterator last, UnaryPredicate pred);
在範圍內查詢元素(否定條件)
返回一個迭代器,指向範圍 [first,last) 中第一個使得 pred 返回 false 的元素。如果沒有找到這樣的元素,函式將返回 last

此函式模板的行為等同於
1
2
3
4
5
6
7
8
9
template<class InputIterator, class UnaryPredicate>
  InputIterator find_if_not (InputIterator first, InputIterator last, UnaryPredicate pred)
{
  while (first!=last) {
    if (!pred(*first)) return first;
    ++first;
  }
  return last;
}

引數

first, last
輸入迭代器,指向序列的初始和末尾位置。使用的範圍是 [first,last),它包含 firstlast 之間的所有元素,包括 first 指向的元素,但不包括 last 指向的元素。
pred
一元函式,它接受範圍內的元素作為引數,並返回一個可轉換為 bool 的值。返回值表明該元素是否在此函式的上下文中被視為匹配項。
該函式不得修改其引數。
這既可以是函式指標,也可以是函式物件。

返回值

指向範圍中第一個使得 pred 返回 false 的元素的迭代器。
如果 pred 對所有元素都返回 true,則函式返回 last

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// find_if_not example
#include <iostream>     // std::cout
#include <algorithm>    // std::find_if_not
#include <array>        // std::array

int main () {
  std::array<int,5> foo = {1,2,3,4,5};

  std::array<int,5>::iterator it =
    std::find_if_not (foo.begin(), foo.end(), [](int i){return i%2;} );
  std::cout << "The first even value is " << *it << '\n';

  return 0;
}

輸出
The first even value is 2


複雜度

最多線性於 firstlast 之間的 距離:對每個元素呼叫 pred,直到找到不匹配為止。

資料競爭

範圍 [first,last) 中的一些(或全部)物件被訪問(最多一次)。

異常

如果 pred 或迭代器上的操作丟擲異常,則丟擲異常。
請注意,無效的引數會導致 *未定義行為*。

另見