4

How can I determine the largest number among three numbers using C++?

I need to simplify this

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

Conditionals do not simplify this.

trafalgarLaww
  • 505
  • 6
  • 15
  • 4
    Define "simplify". If it's a code golf problem you should post it here: https://codegolf.stackexchange.com/ – XCS Nov 21 '17 at 13:37
  • Possible duplicate of [Find maximum of three number in C without using conditional statement and ternary operator](https://stackoverflow.com/questions/7074010/find-maximum-of-three-number-in-c-without-using-conditional-statement-and-ternar) – Neha Nov 21 '17 at 13:44
  • `(x > y) ? (x > z ? x : z) : (y > z ? y : z);` is better ordering of the brackets.. There is also `std::max_element(std::begin(array), std::end(array))` which would give you the position in the array of the maximum element. – Brandon Nov 21 '17 at 13:48
  • @NehaGupta Not a dupe; that question is for C – Justin Nov 22 '17 at 22:23

5 Answers5

20

Starting from C++11, you can do

w = std::max({ x, y, z });
oisyn
  • 1,306
  • 5
  • 14
  • @Bathsheba - But (and it's a big one) yours preforms no copies. This one I'd wager copies all the elements (though I won't encourage anyone to optimize prematurely based on this). – StoryTeller - Unslander Monica Nov 21 '17 at 13:48
  • 1
    @StoryTeller: Careful: I've already upvoted 2 answers of yours today. – Bathsheba Nov 21 '17 at 13:48
  • I guess! Stackoverflow is turning to trading reputation website! :D – Neha Nov 21 '17 at 13:53
  • @StoryTeller That argument works both ways. In the two-argument version of `std::max`, arguments are passed by references. For built-in numeric types or small POD types, passing the value is usually cheaper than passing a reference and dereferencing the references in the function itself. In practice, it will probably all be inlined. But yes, good point, this is something worth considering. – oisyn Nov 21 '17 at 13:55
  • 2
    @NehaGupta - Hardly. Bathsheba and I just happen to frequent the same tags, and engage in friendly banter. – StoryTeller - Unslander Monica Nov 21 '17 at 13:56
  • @oisyn - Of course, no disrespect to your answer. I can't really choose between the two myself :) – StoryTeller - Unslander Monica Nov 21 '17 at 13:57
  • Here is the solution I found: `w = ((y > z) || (x > z)) ? ((x > y) ? x: y) :z;`. It is utilizes less operations and very transparent. – trafalgarLaww Nov 21 '17 at 17:12
  • I think they should've made a C++11 style varags version of `::std::max`. – Omnifarious Nov 23 '17 at 03:43
8
w = std::max(std::max(x, y), z);

is one way.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
2

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

Neha
  • 3,456
  • 3
  • 14
  • 26
0

Use the simple if condition

int w = x;

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

Where w is the max among three.

Siraj Alam
  • 9,217
  • 9
  • 53
  • 65
0

A variant on oisyn's answer (use an initializer list) and Bathesheba's answer (invoke no copies) is to use std::ref to create an initializer list of references, and then use std::max normally:

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

This is only advantageous if creating a reference is cheaper than creating a copy (and it isn't for primitives like int)

Demo

Community
  • 1
  • 1
AndyG
  • 39,700
  • 8
  • 109
  • 143