C++ 中的 logical_and


在本文中,我们将讨论 C++ 中 logical_and 函数对象类的作用、语法和示例。

logical_and 是什么?

logical_and 二元函数是 C++ 中的一个内置的二元函数对象类,定义在 <functional> 头文件中。logical_and 是二元函数,用于提供两个参数之间逻辑“与”运算的结果。

逻辑 AND 是二元运算,只有当两个二进制值都为真时才返回真。

logical_and 的语法

Template struct logical_and : binary_function
{
   T operator() (const T& a, const T& b) const {return a&b&; }
};

模板参数

该函数接受以下参数 −

  • T − 这是传递给函数调用的参数的类型。

示例

 交互式演示

#include <bits/stdc++.h>
using namespace std;
int main(){
   bool a[] = { true, false, true, false, true };
   bool b[] = { true, true, false, false, true };
   int ele = 5;
   bool output[ele];
   transform(a, a + ele, b, output, logical_and<bool>());
   cout<<"The result for Logical AND is: \n";
   for (int i = 0; i < ele; i++){
      cout << a[i] << " AND " << b[i] << " is: " <<output[i] << "\n";
   }
   return 0;
}

输出

如果我们运行以上代码,它将生成以下输出 −

The result for Logical AND is:
1 AND 1 is: 1
0 AND 1 is: 0
1 AND 0 is: 0
0 AND 0 is: 0
1 AND 1 is: 1

更新于: 22-Apr-2020

75 次浏览

启动你的职业

通过完成课程获得认证

开始
广告
© . All rights reserved.