c++ - More concise way to write the following statement -
is there more concise way write following c++ statements:
int max = 0; int u = up(); if(u > max) { max = u; } int d = down(); if(d > max) { max = d; } int r = right(); max = r > max ? r : max;
specifically there way embed assignment of functions return inside if statement/ternary operator?
assuming that:
- the idea remove local variables (i.e. don't need
u
,d
,r
later on) - evaluation order doesn't matter
... can use std::max
:
int m = max(max(max(0, up()), down()), right());
if return value of function:
return max(max(max(0, up()), down()), right());
note that can evaluate functions in order, rather string up, down, right order in original code.
Comments
Post a Comment