几天前我遇到了这个谜语
main ()
{
cout << (1 < 1 < 1 ? "true" : "false") << endl;
}
我不明白为什么它会返回 true。我原以为是假的。
更多:
cout << (2 < 2 < 2 ? "true" : "false") << endl; // returns true
cout << (0 < 0 < 0 ? "true" : "false") << endl; // returns false
cout << (-1 < -1 < -1 ? "true" : "false") << endl; // returns false
cout << (-2 < -2 < -2 ? "true" : "false") << endl; // returns false
它似乎对严格大于 0 的值返回 true,否则返回 false。
有人对此有解释吗?提前致谢
最佳答案
表达式1 < 1 < 1
被解析为
(1 < 1) < 1
在这里,1 < 1
是false
, 转换为 0
, 屈服
(0) < 1
这是真的。
关于c++ - 为什么 (1 < 1 < 1) 在 C++ 中返回 true?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32022709/