gpt4 book ai didi

c++ - bool 方程

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:06:51 29 4
gpt4 key购买 nike

为什么这段代码有两个不同的输出(GCC 4.5.1)(我已经评论了重要的行):

int main()
{
bool a = 1;
bool b = 1;
bool c = 1;
bool a_or_b = (a || b);
bool not_a_or_b = !a_or_b;
bool not_a_or_b__c = not_a_or_b || c;
cout << "(a || b): " << (a || b) << '\n';
cout << "!(a || b): " << !(a || b) << '\n';
cout << "!(a || b) || c: " << (!(a || b)) || c << '\n';//HERE I'M GETTING 0 (incorrectly I would say)
cout << "bool vars:\n";//WHY THIS LINE IS PRINTED AFTER THE PREVIOUS LINE BUT NOT BELOW IT?
cout << "(a || b): " << a_or_b << '\n';
cout << "!(a || b): " << not_a_or_b << '\n';
cout << "!(a || b) || c: " << not_a_or_b__c << '\n';//HERE I'M GETTING 1
return 0;
}

最佳答案

这是因为 <<优先级高于 || .使用括号对其进行分组。

cout << "!(a || b) || c: " << ((!(a || b)) || c) << '\n';
// ^ ^

关于c++ - bool 方程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5297166/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com