ในบทความนี้ เราจะพูดถึงการทำงาน ไวยากรณ์ และตัวอย่างของคลาสอ็อบเจ็กต์ฟังก์ชัน logical_and ใน C++
ตรรกะ_และคืออะไร
ฟังก์ชัน logical_and binary เป็นคลาสอ็อบเจ็กต์ฟังก์ชันไบนารี inbuilt ใน C++ ซึ่งกำหนดไว้ในไฟล์ส่วนหัว
ตรรกะ AND คือการดำเนินการไบนารีที่คืนค่า true เท่านั้นและต่อเมื่อค่าไบนารีทั้งสองเป็นจริง
ไวยากรณ์ของ logical_and
Template struct logical_and : binary_function
{
T operator() (const T& a, const T& b) const {return a&b&; }
}; พารามิเตอร์เทมเพลต
ฟังก์ชันยอมรับพารามิเตอร์ต่อไปนี้ −
-
ท − นี่คือประเภทของอาร์กิวเมนต์ที่ส่งผ่านไปยังการเรียกใช้ฟังก์ชัน
ตัวอย่าง
#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