条件运算符总是可以用 if/else 代替吗?
到目前为止,我一直在思考条件运算符 int a = b == 2 ? x1 : x2;
始终可以用 if/else 语句替换。
int a;
if (b == 2)
a = x1;
else
a = x2;
两者之间的选择始终取决于品味。今天,我正在处理一项任务,如果我可以写:
int& a;
if (b == 2)
a = x1;
else
a = x2;
这是不允许的,那么引用将很有用,并且我尝试使用条件运算符初始化引用。这是有效的,我开始意识到,条件运算符并不总是可以被 if/else 语句替换。
我的这个结论对吗?
Until now I was thinking the conditional operator int a = b == 2 ? x1 : x2;
is always replaceable by an if/else statement.
int a;
if (b == 2)
a = x1;
else
a = x2;
And the choice between one of the two is always a matter of taste. Today I was working with a task where a reference would be useful if I could write:
int& a;
if (b == 2)
a = x1;
else
a = x2;
This is not allowed and I tried the initialization of the reference with the conditional operator. This was working and I came to realize, that the conditional operator is not always replaceable by an if/else statement.
Am I right with this conclusion?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
你是对的。条件运算符是一个表达式,而
if-else
是一个语句。可以使用语句的地方可以使用表达式,但反之则不然。当您遇到有人坚持认为您永远不应该使用条件表达式时,这是一个很好的反例,因为
if-else
是“简单”而条件“太复杂” 。当 C++ 获得 lambda 表达式时,您可以使用带有
if-else
的 lambda 来代替条件。You are correct. The conditional operator is an expression, whereas
if-else
is a statement. An expression can be used where a statement can be used, but the opposite is not true.This is a good counterexample to show when you come across somebody who insists that you should never, never, never, ever use conditional expressions, because
if-else
is "simple" and conditionals are "too complicated".When C++ gets lambda expressions, then you may be able to use a lambda with an
if-else
in place of a conditional.嗯,显然有很多地方你不能放置“如果”。例如:
没有办法用 if 语句来编写它。这是一个骗局,好几次,我并不是责怪你没有找到它,因为我找不到。
Well, there are obviously lots of places that you can't place an if. For example:
There is no way of writing that with an if statement. And this is a dupe, several times, not that I blame you for not finding it, because I can't.
不完全是。您始终可以用指针(可重新安装)替换参考(不可重新安装)。所以这是一个上下文问题。
例如,你可以写
“没问题”,正如有人曾经对终结者所说的那样。
并全力以赴以获得最大的“呃,那是什么”效果,
但在这种情况下使用条件运算符更清楚。 :-)
干杯&呵呵,
Not exactly. You can always replace the reference (not re-seatable) with a pointer (re-seatable). So it's a matter of context.
E.g. you can write
No problemo, as someone once remarked to the Terminator.
And going all out for maximum "ugh what's that" effect,
But it's more clear with the conditional operator in this case. :-)
Cheers & hth.,
你将会遇到更多的问题
You are going to have more problems than that
你是对的。
在 C++ 中,存在无法使用 if-else 语句的条件赋值情况,因为该语言明确区分初始化和赋值。
此外,三元运算符可以产生左值,即可以向其分配另一个值的值。
此外,某些编译器在某些情况下可能会为三元与条件 if-then 生成不同的代码。例如,如果使用三元运算符,GCC 会执行更好的代码优化。
另请参见
?:
C++ 中的三元运算符< /a>.You are right.
In C++ there are conditional assignment situations where use of the if-else statement is impossible, since this language explicitly distinguishes between initialization and assignment.
Furthermore, the ternary operator can yield an lvalue, i.e. a value to which another value can be assigned.
Also, some compilers in some cases may generate different code for ternary vs conditional if-then. GCC, for example, performs better code optimization if ternary operator is used.
See also
?:
ternary operator in C++.您不能直接使用它,但您始终可以通过将条件转换为作为表达式求值的内容来绕过该限制...
当然,这对于整数来说可能有点过大了。
You can't use it directly, but you can always get around that restriction by turning your conditional into something that is evaluated as an expression...
Of course, this may be overkill for ints.
这取决于您对可替换的定义。例如,在单个函数调用中,我无法用
if-else
替换以下条件运算符。然而,通过添加另一个函数,我有效地用
if-else
替换了条件运算符。It depends on your definition of replaceable. For example, within a single function call, I cannot replace the following conditional operator with an
if-else
.However, with the addition of another function, I've effectively replaced the conditional operator with an
if-else
.根据 kodify.net,我们无法用条件运算符替换每个
if/else
语句。这样做有两个要求:if 和 else 块中必须有一个表达式。Acording to kodify.net we cannot replace every
if/else
statement with the conditional operator. There are two requirements for doing so: there must be one expression in both the if and the else block.