Determine the largest number among three numbers using C ++

How to determine the largest number of three numbers using C ++?

I need to simplify this

 w=(z>((x>y)?x:y)?z:((x>y)?x:y));

Conditional expressions do not simplify this.

+4
source share
5 answers

Starting with C ++ 11, you can do

w = std::max({ x, y, z });
+18
source
w = std::max(std::max(x, y), z);

- one of the methods.

+8
source

big = a > b? (a > c? a: c): (b > c? b: c);

+2

if

int w = x;

if(y > w)
  w = y;
if(z > w)
  w = z;

w .

0

oisyn answer ( ) Bathesheba ( ) std::ref , std::max:

using std::ref;
w = std::max({ref(x), ref(y), ref(z)});

This is only beneficial if link creation is cheaper than copying (and this is not for type primitives int)

Demo

0
source

Source: https://habr.com/ru/post/1689626/


All Articles