function template
<functional>

std::not1

template <class Predicate>  unary_negate<Predicate> not1 (const Predicate& pred);
返回一元函式物件的否定
構造一個一元函式物件(型別為 unary_negate),該物件返回 pred 的相反值(由運算子 ! 返回)。

它的定義與以下行為相同:
1
2
3
4
template <class Predicate> unary_negate<Predicate> not1 (const Predicate& pred)
{
  return unary_negate<Predicate>(pred);
}

引數

pred
定義了成員 argument_type 的一元函式物件類型別。

返回值

pred 行為相反的一元函式物件。
參見 unary_negate

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// not1 example
#include <iostream>     // std::cout
#include <functional>   // std::not1
#include <algorithm>    // std::count_if

struct IsOdd {
  bool operator() (const int& x) const {return x%2==1;}
  typedef int argument_type;
};

int main () {
  int values[] = {1,2,3,4,5};
  int cx = std::count_if (values, values+5, std::not1(IsOdd()));
  std::cout << "There are " << cx << " elements with even values.\n";
  return 0;
}

輸出

There are 2 elements with even values.


另見