Mar*_*ian 1 c++ cout conditional-operator
这是我的代码:
std::cout << "The contaner is " << (!container)?"not":; "empty";
Run Code Online (Sandbox Code Playgroud)
这显然不起作用,但我希望现在这个想法很清楚。我想打印"The container is empty",并"not"在"empty"if bool containeris之前添加false。
我想知道是否有可能,或者我是否必须按照以下方式写一些东西:
if(container) std::cout ...;
else std::cout ...;
Run Code Online (Sandbox Code Playgroud)
当所有其他方法都失败时,只需使用 if 语句:
std::cout << "The contaner is ";
if (!container)
std::cout << "not ";
std::cout<< "empty";
Run Code Online (Sandbox Code Playgroud)
就我个人而言,我更喜欢使用条件运算符,因为它更容易阅读。当您要显示的内容类型不同时,这也适用。条件运算符要求将两种情况都转换为通用类型,因此类似的操作!container ? "not" : 1不起作用。
| 归档时间: |
|
| 查看次数: |
114 次 |
| 最近记录: |