Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C++

logical_and trong C++

std::logical_and là function object trong <functional> thực hiện phép AND logic trên 2 đối số. Trả về true chỉ khi cả 2 đều true.

Cú pháp

template <class T> struct logical_and : binary_function<T,T,bool> {
    bool operator()(const T& a, const T& b) const { return a && b; }
};

Ví dụ

#include <bits/stdc++.h>
using namespace std;

int main() {
    bool a[] = {true, false, true, false, true};
    bool b[] = {true, true, false, false, true};
    bool out[5];
    transform(a, a+5, b, out, logical_and<bool>());
    for (int i = 0; i < 5; i++)
        cout << a[i] << " AND " << b[i] << " is: " << out[i] << "\n";
    return 0;
}

Đầu ra

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