Equality operators ใน C++ คือ is equal to (==) และ is not equal to (!=) พวกมันทำงานตามชื่อของมัน binary equality operators เปรียบเทียบ operands สำหรับความเท่ากันหรือไม่เท่ากันอย่างเคร่งครัด Equality operators มี precedence ต่ำกว่า relational operators แต่พฤติกรรมคล้ายกัน ผลลัพธ์เป็น bool
Equal-to operator (==) คืน true (1) ถ้าทั้งสอง operands มีค่าเท่ากัน มิฉะนั้นคืน false (0) Not-equal-to operator (!=) คืน true ถ้า operands ไม่มีค่าเท่ากัน มิฉะนั้นคืน false
ตัวอย่าง
#include <iostream>
using namespace std;
int main() {
cout << boolalpha // พิมพ์ true/false แทน 1/0
<< "The true expression 3 != 2 yields: "
<< (3 != 2) << endl
<< "The false expression 20 == 10 yields: "
<< (20 == 10) << endl;
}
ผลลัพธ์
The true expression 3 != 2 yields: true The false expression 20 == 10 yields: false