gpt4 book ai didi

c++ - 三元运算符中的 Cout String 和 int

转载 作者:太空宇宙 更新时间:2023-11-04 14:50:00 30 4
gpt4 key购买 nike

我的 c++ 代码中有一行是这样的:

cout<<(i%3==0 ? "Hello\n" : i) ;//where `i` is an integer.

但是我得到这个错误:

operands to ?: have different types 'const char*' and 'int

如何修改代码(最少字符数)?

最佳答案

丑陋的:

i%3==0 ? cout<< "Hello\n" : cout<<i;

不错:

if ( i%3 == 0 )
cout << "Hello\n";
else
cout << i;

您的版本不起作用,因为 : 两边表达式的结果类型需要兼容。

关于c++ - 三元运算符中的 Cout String 和 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14649845/

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