cout如何<< (i> 0);使它等于1?

时间:2016-03-04 20:41:52

标签: c++ cout

在声明cout << (i>0)中,其中i为正,且大于0,(? > 0)总是有多大的影响(据我测试) 1?

#include <iostream>

using namespace std;

int main()
{
    int i = 2, j = 5;
    double k = 5.0;
    cout << (i%j > 0) << " " << (i/k >0) << " " << (100>0) << endl;
    cout << (i%j) << " " << (i/k) << endl;
    return 0;
}

输出:

1 1 1
2 0.4    

3 个答案:

答案 0 :(得分:0)

(i>0)返回布尔值true / false,然后cout自动将其输入1或0。

答案 1 :(得分:0)

您在perentheses中使用的表达式被评估为&#39; true&#39; &#39; false&#39; ,因此当表达式为评估为真输出为1,如果为假,则输出为0.如果需要,可以启用boolalpha,如下所示:

cout << boolalpha << (i % j > 0) << (i / k > 0);// rest of code...

这个的输出是:

true false

答案 2 :(得分:-3)