logical_and in C++

logical_and in C++ is a binary function object class which returns the result of the logical “and” operation between its two arguments (as returned by operator &&).


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

(T)Type of the arguments and the return type of the function call.

Member types:

  • a: Type of the first argument in member operator()
  • b: Type of the second argument in member operator()
  • result_type: Type returned by member operator()

Below is the program to implement logical_and using std::transform():





#include <bits/stdc++.h>
using namespace std;
int main()
    bool z[] = { true, false, true, false, true };
    bool y[] = { true, true, false, false, true };
    int n = 5;
    bool result[n];
    // using transform to
    // perform logical_AND on two array
    transform(z, z + n, y, result,
    cout << "Logical AND:\n";
    for (int i = 0; i < n; i++)
        cout << z[i] << " AND " << y[i]
             << " = " << result[i] << "\n";
    return 0;



Logical AND:
1 AND 1 = 1
0 AND 1 = 0
1 AND 0 = 0
0 AND 0 = 0
1 AND 1 = 1

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.