开发者

C++ Conditional Operator

I once seen a -wired- operator in C++ which assigns value 开发者_如何学Cif greater than..

it was a combination of ?, < and =

e.g. let x = value if value is greater than x

I do not mean x=(x<value)x:value

It was some sort of x<?=value

But I can not remember it exactly, and can not find it online... Can some one remind me of it?

Thanks,


There is no operator that assigns variables based on their relative values.

However, there is the ?: operator:

x = value > x ? value : x;

If you read it out loud from left to right, it makes sense.


gcc has -- in version 3.3.6 at least! -- a gcc-specific language extension providing specialized operators for implementing min and max. Perhaps this is what you are thinking of?

Minimum and Maximum Operators in C++

I don't have gcc handy to test it with, but it might have an updating form, too.


How's that:

(x<value) || (x=value)


Are you thinking of the ternary operator?

result = a > b ? x : y;


I suspect what you're thinking of is a gcc extension1 that lets you leave out the middle operand to the conditional operator, so (for example):

a = b ? b : c;

can be written as:

a = b ?: c;

1 Despite the '2.95.3' in the URL, I'm not aware of a newer version of the linked page. If somebody is, please feel free to point it out (or edit it in).


it's a more convenient version of an if statement that is used for assignment

int x = (some bool) ? trueval : falseval;

this is roughly what it means, when the bool is evaluated it either gets the trueval or falseval depending on the outcome, it's easier than saying

int x;
if (someval)
    x = trueval;
else
    x = falseval;


x = x < value ? 0 : 1;

This function sets x to 0 is x < value, 1 otherwise.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜