A min= idiom in C++?

后端 未结 7 815
旧巷少年郎
旧巷少年郎 2021-02-09 22:10

We use

x += y

instead of

x = x + y

And similarly for *,/,- and other operators. Well, what about

7条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-09 23:03

    The options you have:

    x = std::min(x,y)
    

    or

    x = y < x ? y : x;
    

    or

    if (y < x) x = y;
    

提交回复
热议问题