類模板
<functional>

std::logical_not

template <class T> struct logical_not;
邏輯非函式物件類
一元函式物件類,其呼叫返回其引數的邏輯“非”操作的結果(由 operator ! 返回)。

泛指,函式物件 是一個類的例項,該類定義了成員函式operator()。這個成員函式允許物件以與函式呼叫相同的語法使用。

它的定義與以下行為相同:

1
2
3
template <class T> struct logical_not : unary_function <T,bool> {
  bool operator() (const T& x) const {return !x;}
};
1
2
3
4
5
template <class T> struct logical_not {
  bool operator() (const T& x) const {return !x;}
  typedef T argument_type;
  typedef bool result_type;
};

模板引數

T
傳遞給函式呼叫的引數的型別。
該型別應支援運算(operator!)。

成員型別

成員型別定義說明
argument_typeT成員 operator() 中引數的型別
result_typebool成員 operator() 返回的型別

成員函式

bool operator() (const T& x)
返回其引數的邏輯非!x)的成員函式。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// logical_not example
#include <iostream>     // std::cout, std::boolalpha
#include <functional>   // std::logical_not
#include <algorithm>    // std::transform

int main () {
  bool values[] = {true,false};
  bool result[2];
  std::transform (values, values+2, result, std::logical_not<bool>());
  std::cout << std::boolalpha << "Logical NOT:\n";
  for (int i=0; i<2; i++)
    std::cout << "NOT " << values[i] << " = " << result[i] << "\n";
  return 0;
}

輸出

Logical NOT:
NOT true = false
NOT false = true


另見