function template
<algorithm>

std::none_of

template <class InputIterator, class UnaryPredicate>  bool none_of (InputIterator first, InputIterator last, UnaryPredicate pred);
測試是否沒有元素滿足條件
如果範圍 [first,last) 中的所有元素 pred 都返回 false,或者範圍為空,則返回 true,否則返回 false

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

引數

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

返回值

如果範圍 [first,last) 中的所有元素 pred 都返回 false,或者範圍為空,則返回 true,否則返回 false

示例

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

int main () {
  std::array<int,8> foo = {1,2,4,8,16,32,64,128};

  if ( std::none_of(foo.begin(), foo.end(), [](int i){return i<0;}) )
    std::cout << "There are no negative elements in the range.\n";

  return 0;
}

輸出
There are no negative elements in the range.


複雜度

最多與 firstlast 之間的 距離 成線性關係:對每個元素呼叫 pred 直到找到匹配項。

資料競爭

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

異常

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

另見