C++ Conditional ? : Operator

c++代码写很多小伙伴都能写,但是如何efficient并且effective?这个应该是还蛮困难的,LZ只能说可以写C++,但是代码不够优雅,还需要继续修炼O(∩_∩)O哈哈~

介绍一个比较好的表达,可以代替if-else

Exp1 ? Exp2 : Exp3;

如果Exp1为真,则Exp2,否则为Exp3.

例子如下:

#include <iostream>
using namespace std;

int main(){
    int x, y = 10;
    x = (y<10)?30:40;
    cout << "value of x : " << x << endl;

    return 0;
}
value of x : 40

(^o^)/~

猜你喜欢

转载自blog.csdn.net/felaim/article/details/80171419