C++ 速查手冊 V1.00 - 單元 4.3 - 關係運算




C++ 的關係運算子 (relational operator) 都需要兩個運算元,包含小於、小於等於、大於、大於等於,另外有兩個測試相等性的運算子,運算結果會是 bool 型態的數值,不是 true 就是 false


運算子 功能 範例
< 小於 a < b
<= 小於等於 a <= b
> 大於 a > b
>= 大於等於 a >= b
== 相等 a == b
!= 不等 a != b

關鍵字 eq 的用法如同 ==not_eq 的用法如同 !=

以下為整數型態做關係及相等性運算的例子


001 #include <iostream>
002
003 #int main() {
004    int a = 12;
005    int b = 22;
006
007    if (a < b) {
008       std::cout << "a < b"
009                 << std::endl;
010    }
011
012    if (a <= b) {
013       std::cout << "a <= b"
014                 << std::endl;
015    }
016
017    if (a > b) {
018       std::cout << "a > b"
019                 << std::endl;
020    }
021
022    if (a >= b) {
023       std::cout << "a >= b"
024                 << std::endl;
025    }
026
027    if (a == b) {
028       std::cout << "a == b"
029                 << std::endl;
030    }
031
032    if (a != b) {
033       std::cout << "a != b"
034                 << std::endl;
035    }
036
037    return 0;
038 }
039
040 /* Kaiching Chang
041    u0403_1.cpp
042    2014-02 */

編譯後執行,結果如下


$ g++ u0403_1.cpp
$ ./a.out
a < b
a <= b
a != b
$

以下為浮點數型態做關係及相等性運算的例子


001 #include <iostream>
002
003 int main() {
004    double a = 22.0;
005    double b = 22.0;
006
007    if (a < b) {
008       std::cout << "a < b"
009                 << std::endl;
010    }
011
012    if (a <= b) {
013       std::cout << "a <= b"
014                 << std::endl;
015    }
016
017    if (a > b) {
018       std::cout << "a > b"
019                 << std::endl;
020    }
021
022    if (a >= b) {
023       std::cout << "a >= b"
024                 << std::endl;
025    }
026
027    if (a == b) {
028       std::cout << "a == b"
029                 << std::endl;
030    }
031
032    if (a != b) {
033       std::cout << "a != b"
034                 << std::endl;
035    }
036
037    return 0;
038 }
039
040 /* Kaiching Chang
041    u0403_2.cpp
042    2014-02 */

編譯後執行,結果如下


$ g++ u0403_2.cpp
$ ./a.out
a <= b
a >= b
a == b
$

字元型態可依 ASCII 編碼順序,也就是各編碼代表的整數值進行關係運算或相等性測試,如


001 #include <iostream>
002
003 int main() {
004    char a = 't';
005    char b = 'r' + 2;
006
007    if (a < b) {
008       std::cout << "a < b"
009                 << std::endl;
010    }
011
012    if (a <= b) {
013       std::cout << "a <= b"
014                 << std::endl;
015    }
016
017    if (a > b) {
018       std::cout << "a > b"
019                 << std::endl;
020    }
021
022    if (a >= b) {
023       std::cout << "a >= b"
024                 << std::endl;
025    }
026
027    if (a == b) {
028       std::cout << "a == b"
029                 << std::endl;
030    }
031
032    if (a != b) {
033       std::cout << "a != b"
034                 << std::endl;
035    }
036
037    return 0;
038 }
039
040 /* Kaiching Chang
041    u0403_3.cpp
042    2014-02 */

編譯後執行,結果如下


$ g++ u0403_3.cpp
$ ./a.out
a <= b
a >= b
a == b
$

布林型態的變數也可以做關係相等性測試,注意布林值屬於算術型態 (arithmetic type) ,因此 true 類似整數 1 ,而 false 類似整數 0 。範例程式如下


001 #include <iostream>
002
003 int main() {
004    bool a = true;
005    bool b = false;
006
007    if (a < b) {
008       std::cout << "a < b"
009                 << std::endl;
010    }
011
012    if (a <= b) {
013       std::cout << "a <= b"
014                 << std::endl;
015    }
016
017    if (a > b) {
018       std::cout << "a > b"
019                 << std::endl;
020    }
021
022    if (a >= b) {
023       std::cout << "a >= b"
024                 << std::endl;
025    }
026
027    if (a == b) {
028       std::cout << "a == b"
029                 << std::endl;
030    }
031
032    if (a != b) {
033       std::cout << "a != b"
034                 << std::endl;
035    }
036
037    return 0;
038 }
039
040 /* Kaiching Chang
041    u0403_4.cpp
042    2014-02 */

編譯後執行,結果如下


$ g++ u0403_4.cpp
$ ./a.out
a > b
a >= b
a != b
$

continue ...

沒有留言: