【问题标题】:What does: "while( (stuff) ? false : (otherstuff)){}" mean? [duplicate]“while( (stuff) ? false : (otherstuff)){}” 是什么意思? [复制]
【发布时间】:2016-08-29 05:49:57
【问题描述】:

我正在查看一个 c++ while 循环:

while ((stuff) ? false : (otherstuff))
{
  commands;
}

我真的不明白它试图用“?false :”部分做什么? 谁能解释一下这是什么意思? 我已经尝试查找它,但我并没有真正得到任何帮助。

【问题讨论】:

  • 你知道?:是什么运算符吗?
  • while(!stuff && otherstuff) 的说法很复杂
  • 不,我不知道。我以前从未使用过。

标签: c++ ternary-operator


【解决方案1】:

它使用ternary conditional operator 来有效地执行检查:

while (!(stuff) && (otherstuff))

如果stuff 为真,则计算三元组的第一个选项(计算为false),如果为假,则计算为其他选项。

【讨论】:

    【解决方案2】:

    这只是写这个的一种非常糟糕的方式:

    while (!stuff && otherstuff) {
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-12
      • 2015-01-22
      • 2014-11-05
      • 2018-03-03
      • 2021-12-10
      • 2020-10-27
      • 2015-07-15
      相关资源
      最近更新 更多