什么是'?'在C ++中做? [英] What does '?' do in C++?

查看:173
本文介绍了什么是'?'在C ++中做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int qempty()
{
    return (f == r ? 1 : 0);
}

在上述代码段中,? 意思?我们可以用什么替换它?

In the above snippet, what does "?" mean? What can we replace it with?

推荐答案

这通常被称为条件运算符 ,并且在使用时如下:

This is commonly referred to as the conditional operator, and when used like this:

condition ? result_if_true : result_if_false

...如果条件计算 true ,表达式计算为 result_if_true ,否则计算为 result_if_false

... if the condition evaluates to true, the expression evaluates to result_if_true, otherwise it evaluates to result_if_false.

这是句法糖,在这种情况下,它可以替换为

It is syntactic sugar, and in this case, it can be replaced with

int qempty()
{ 
  if(f == r)
  {
      return 1;
  } 
  else 
  {
      return 0;
  }
}

注意:请将?:作为三元运算符,因为它是语言中唯一的三元运算符他们正在使用。

Note: Some people refer to ?: it as "the ternary operator", because it is the only ternary operator (i.e. operator that takes three arguments) in the language they are using.

这篇关于什么是'?'在C ++中做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆