'?' 是什么意思?在 C++ 中做什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/795286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:18:38  来源:igfitidea点击:

What does '?' do in C++?

c++operatorsternary-operator

提问by Thaier Alkhateeb

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

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

在上面的片段中,“ ”是什么意思?我们可以用什么来代替它?

回答by Daniel LeCheminant

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

这通常被称为条件运算符,当像这样使用时:

condition ? result_if_true : result_if_false

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

...如果condition计算为true,则表达式计算为result_if_true,否则计算为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.

注意:有些人将?:其称为“三元运算符”,因为它是他们使用的语言中唯一的三元运算符(即带有三个参数的运算符)。

回答by Richard

This is a ternary operator, it's basically an inline if statement

这是一个三元运算符,它基本上是一个内联 if 语句

x ? y : z

works like

if(x) y else z

except, instead of statements you have expressions; so you can use it in the middle of a more complex statement.

除了,你有表达式而不是语句;所以你可以在更复杂的语句中间使用它。

It's useful for writing succinct code, but can be overused to create hard to maintain code.

它对于编写简洁的代码很有用,但可能会被过度使用以创建难以维护的代码。

回答by Richard

You can just rewrite it as:

您可以将其重写为:

int qempty(){ return(f==r);}

Which does the same thing as said in the other answers.

这与其他答案中所说的相同。

回答by Nick Kossifidis

Just a note, if you ever see this:

请注意,如果您看到此信息:

a = x ? : y;

It's a GNU extension to the standard (see https://gcc.gnu.org/onlinedocs/gcc/Conditionals.html#Conditionals).

它是标准的 GNU 扩展(请参阅https://gcc.gnu.org/onlinedocs/gcc/Conditionals.html#Conditionals)。

It is the same as

它与

a = x ? x : y;

回答by jjnguy

It is called the conditional operator.

它被称为条件运算符

You can replace it with:

您可以将其替换为:

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

回答by Joe

It's the conditional operator.

这是条件运算符。

a ? b : c

一种 ?乙:丙

It's a shortcut for IF/THEN/ELSE.

这是 IF/THEN/ELSE 的快捷方式。

means: if a is true, return b, else return c. In this case, if f==r, return 1, else return 0.

意思是:如果a为真,返回b,否则返回c。在这种情况下,如果 f==r,则返回 1,否则返回 0。

回答by ssakl

The question mark is the conditional operator. The code means that if f==r then 1 is returned, otherwise, return 0. The code could be rewritten as

问号是条件运算符。代码意味着如果 f==r 则返回 1,否则返回 0。代码可以重写为

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

which is probably not the cleanest way to do it, but hopefully helps your understanding.

这可能不是最干净的方法,但希望能帮助您理解。