条件运算符总是可以用 if/else 代替吗?

发布于 2024-10-08 08:19:32 字数 372 浏览 8 评论 0原文

到目前为止,我一直在思考条件运算符 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

谁人与我共长歌 2024-10-15 08:19:32

你是对的。条件运算符是一个表达式,而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.

手心的温暖 2024-10-15 08:19:32

嗯,显然有很多地方你不能放置“如果”。例如:

func( x ? 0 : 1 );

没有办法用 if 语句来编写它。这是一个骗局,好几次,我并不是责怪你没有找到它,因为我找不到。

Well, there are obviously lots of places that you can't place an if. For example:

func( x ? 0 : 1 );

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.

世俗缘 2024-10-15 08:19:32

不完全是。您始终可以用指针(可重新安装)替换参考(不可重新安装)。所以这是一个上下文问题。

例如,你可以写

int* pa;
if( b == 2 )
    pa = &x1;
else
    pa = &x2;
int& a = *pa;

“没问题”,正如有人曾经对终结者所说的那样。

并全力以赴以获得最大的“呃,那是什么”效果,

int* pa;
switch( b == 2 )
{
case true:
    pa = &x1;  break;
default:
    pa = &x2;
}
int& a = *pa;

但在这种情况下使用条件运算符更清楚。 :-)

int& a = (b == 2? x1 : x2);

干杯&呵呵,

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

int* pa;
if( b == 2 )
    pa = &x1;
else
    pa = &x2;
int& a = *pa;

No problemo, as someone once remarked to the Terminator.

And going all out for maximum "ugh what's that" effect,

int* pa;
switch( b == 2 )
{
case true:
    pa = &x1;  break;
default:
    pa = &x2;
}
int& a = *pa;

But it's more clear with the conditional operator in this case. :-)

int& a = (b == 2? x1 : x2);

Cheers & hth.,

月牙弯弯 2024-10-15 08:19:32

你将会遇到更多的问题

// works
ostream *o;
if(x)
  o = &myfiles;
else
  o = &mystrings;


// stringstream* and fstream* -- incompatible!
ostream *o = x ? &myfiles : &mystrings;

You are going to have more problems than that

// works
ostream *o;
if(x)
  o = &myfiles;
else
  o = &mystrings;


// stringstream* and fstream* -- incompatible!
ostream *o = x ? &myfiles : &mystrings;
时光是把杀猪刀 2024-10-15 08:19:32

你是对的。

在 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++.

盗琴音 2024-10-15 08:19:32

您不能直接使用它,但您始终可以通过将条件转换为作为表达式求值的内容来绕过该限制...

int& initValue(int b, int& x1, int& x2){
    if (b==2)
        return x1;
    return x2;
}

...

int& a = initValue(b, x1, x2);

当然,这对于整数来说可能有点过大了。

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...

int& initValue(int b, int& x1, int& x2){
    if (b==2)
        return x1;
    return x2;
}

...

int& a = initValue(b, x1, x2);

Of course, this may be overkill for ints.

一百个冬季 2024-10-15 08:19:32

这取决于您对可替换的定义。例如,在单个函数调用中,我无法用 if-else 替换以下条件运算符。

int n1 = 10;
int n2 = 20;
const int& i = x > 0 ? n1 : n2;

然而,通过添加另一个函数,我有效地用 if-else 替换了条件运算符。

const int& get_i(double x)
{
  if(x > 0)
    return n1;
  else
    return n2;
}

int main()
{
  const int& i = get_i(x);
}

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.

int n1 = 10;
int n2 = 20;
const int& i = x > 0 ? n1 : n2;

However, with the addition of another function, I've effectively replaced the conditional operator with an if-else.

const int& get_i(double x)
{
  if(x > 0)
    return n1;
  else
    return n2;
}

int main()
{
  const int& i = get_i(x);
}
云醉月微眠 2024-10-15 08:19:32

根据 kodify.net,我们无法用条件运算符替换每个 if/else 语句。这样做有两个要求:ifelse 块中必须有一个表达式。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文