Is there a more concise way to write the 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 is there a way to embed the assignment of the functions return inside the if statement/ternary operator?
Assuming that:
u
, d
, r
later on)... then you can just use std::max
:
int m = max(max(max(0, up()), down()), right());
If this is the return value of the function:
return max(max(max(0, up()), down()), right());
Note that that can evaluate the functions in any order, rather than the string up, down, right order in your original code.
#include <algorithm>
using namespace std;
...
int maxn = max(max(up(), down()), right());
If I'm not mistaken.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With