類模板
<functional>

std::logical_or

template <class T> struct logical_or;
邏輯或函式物件類
二進位制函式物件類,其呼叫返回其兩個引數的邏輯“或”操作的結果(由 operator || 返回)。

通常,函式物件是定義了成員函式 operator() 的類的例項。此成員函式允許物件的使用語法與函式呼叫相同。

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

1
2
3
template <class T> struct logical_or : 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_or {
  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)
成員函式,返回其任一引數是否被視為 true (x||y)。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// logical_or example
#include <iostream>     // std::cout, std::boolalpha
#include <functional>   // std::logical_or
#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_or<bool>());
  std::cout << std::boolalpha << "Logical OR:\n";
  for (int i=0; i<4; i++)
    std::cout << foo[i] << " OR " << bar[i] << " = " << result[i] << "\n";
  return 0;
}

輸出

Logical OR:
true OR true = true
false OR true = true
true OR false = true
false OR false = false


另見