C++ 如何使用条件运算符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/392932/
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
How do I use the conditional operator?
提问by TheFlash
I've always wondered how to write the "A ? B : C"
syntax in a C++ compatible language.
我一直想知道如何"A ? B : C"
用 C++ 兼容语言编写语法。
I think it works something like: (Pseudo code)
我认为它的工作原理类似于:(伪代码)
If A > B
C = A
Else
C = B
Will any veteran C++ programmer please help me out?
请任何资深的 C++ 程序员帮助我吗?
回答by lc.
It works like this:
它是这样工作的:
(condition) ? true-clause : false-clause
It's most commonly used in assignment operations, although it has other uses as well. The ternary operator ?
is a way of shortening an if-else clause, and is also called an immediate-if statement in other languages (IIf(condition,true-clause,false-clause)
in VB, for example).
它最常用于赋值操作,尽管它也有其他用途。三元运算符?
是缩短 if-else 子句的一种方式,在其他语言中也称为立即 if 语句(IIf(condition,true-clause,false-clause)
例如在 VB 中)。
For example:
例如:
bool Three = SOME_VALUE;
int x = Three ? 3 : 0;
is the same as
是相同的
bool Three = SOME_VALUE;
int x;
if (Three)
x = 3;
else
x = 0;
回答by magcius
It works like this:
它是这样工作的:
expression ? trueValue : falseValue
Which basically means that if expression
evaluates to true, trueValue will be returned or executed, and falseValue will be returned or evaluated if not.
这基本上意味着如果expression
评估为真,则将返回或执行 trueValue,否则将返回或评估 falseValue。
Remember that trueValue and falseValue will only be evaluated and executed if the expression is true or false, respectively. This behavior is called short circuiting.
请记住,trueValue 和 falseValue 仅在表达式为 true 或 false 时才会被评估和执行。这种行为称为短路。
回答by Mykroft
In c++ there's no actual if part of this. It's called the ternary operator. It's used like this: <boolean statement> ? <result if true> : <result if false>; For your example above it would look like this:
在 C++ 中,没有实际的 if 部分。它被称为三元运算符。它的用法如下: <boolean statement> ? <result if true> : <result if false>; 对于上面的示例,它看起来像这样:
C = A > B ? A : B;
This article on wikipedia also discusses it: http://en.wikipedia.org/wiki/Ternary_operation
维基百科上的这篇文章也讨论了它:http: //en.wikipedia.org/wiki/Ternary_operation
回答by edef
I assume you mean stuff like a = b ? c : d, where b is the condition, c is the value when b is true, and d is the value when b is false.
我假设您的意思是 a = b 之类的东西?c : d,其中b为条件,c为b为真时的值,d为b为假时的值。
回答by Daniel
I would say the ? is a short-cut. However, some "hard-core" programmers tend to say write it out the long way so in future cases, people can easily read and modify code.
我会说?是捷径。然而,一些“硬核”程序员倾向于说把它写得很长,这样在未来的情况下,人们可以很容易地阅读和修改代码。
For example, if you write
例如,如果你写
int a = b<c ? b : c;
Some people claim that it's clearer to write:
有些人声称这样写更清楚:
if(b<c)
a = b;
else
a = c;
Because in future cases, people can catch it. Of course, a simple b<c ? b:c is easy to catch, but sometimes complex operations are put in and it can be hard to spot.
因为在未来的情况下,人们可以抓住它。当然,一个简单的 b<c ? b:c 很容易捕捉,但有时会进行复杂的操作,很难发现。
回答by Robin Hsu
No one seems to mention that a result of conditional operator expression can be an L-value in C++ (But not in C). The following code compiles in C++ and runs well:
似乎没有人提到条件运算符表达式的结果可以是 C++ 中的 L 值(但不是在 C 中)。以下代码在 C++ 中编译并运行良好:
int a, b;
bool cond;
a=1; b=2; cond=true;
(cond? a : b) = 3;
cout << a << "," << b << endl;
The above program prints 3, 2
上面的程序打印 3, 2
Yet if a and b are of different types, it won't work. The following code gives a compiler error:
但是如果 a 和 b 是不同的类型,它就行不通了。以下代码给出了编译器错误:
int a;
double b;
bool cond;
a=1; b=2; cond=true;
(cond? a : b) = 3;
cout << a << "," << b << endl;
回答by HARSH BAJPAI
IT IS QUITE SIMPLE IT'S BASIC SYNTAX IS: expression1?expression2:expression3;
它非常简单它的基本语法是: expression1?expression2:expression3;
If expression 1 is hold true then expression 2 will hold otherwise expression 3 will hold.
如果表达式 1 成立,则表达式 2 成立,否则表达式 3 成立。
example:
例子:
hey=24>2?24:34;
hey=24>2?24:34;
here as condition is true value of 24 will be assigned to it. if it was false then 34 will be assigned to it
在这里,条件为真时,将为其分配 24 的值。如果它是假的,那么 34 将被分配给它
回答by Ravi Kumar Yadav
simply you can write this as
简单地你可以把它写成
C=(A>B)?A:B;
C=(A>B)?A:B;
THIS IS SAME AS:
这与:
if(A>B)
C=A;
else
C=B;