類模板
<functional>

std::logical_and

template <class T> struct logical_and;
邏輯與函式物件類
二元函式物件類,其呼叫返回其兩個引數的邏輯“與”操作的結果(由 operator && 返回)。

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

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

1
2
3
template <class T> struct logical_and : binary_function <T,T,bool> {
  bool operator() (const T& x, const T& y) const {return x&&y;}
};
1
2
3
4
5
6
template <class T> struct logical_and {
  bool operator() (const T& x, const T& y) const {return x&&y;}
  typedef T first_argument_type;
  typedef T second_argument_type;
  typedef bool result_type;
};

模板引數

T
傳遞給函式呼叫的引數的型別。
該型別應支援 (operator&&) 操作。

成員型別

成員型別定義說明
first_argument_typeT成員 operator() 的第一個引數的型別
second_argument_typeT成員 operator() 的第二個引數的型別
result_typebool成員 operator() 返回的型別

成員函式

bool operator() (const T& x, const T& y)
成員函式,返回其兩個引數是否都被視為真(x&&y)。

示例

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

int main () {
  bool foo[] = {true,false,true,false};
  bool bar[] = {true,true,false,false};
  bool result[4];
  std::transform (foo, foo+4, bar, result, std::logical_and<bool>());
  std::cout << std::boolalpha << "Logical AND:\n";
  for (int i=0; i<4; i++)
    std::cout << foo[i] << " AND " << bar[i] << " = " << result[i] << "\n";
  return 0;
}

輸出

Logical AND:
true AND true = true
false AND true = false
true AND false = false
false AND false = false


另見