-4

I'd like to know (just for knowing) if there is a way to execute the following instructions with simpler code: (C++)

if(a > b && a > c && a > d)

is it possible to replace it with something like this:

 if(a > b, c, d)
YaRmgl
  • 356
  • 1
  • 6
  • 19

3 Answers3

9

Use std::max(std::initializer_list<T>) from the <algorithm> header like follows:

#include <iostream>
#include <algorithm>

int main()
{
    if(4 > std::max({2,3,6}))
        std::cout << "greater\n";
    else
        std::cout << "not greater\n";
}
Ruslan
  • 18,162
  • 8
  • 67
  • 136
1

When you code if(a > b, c, d) you are using the comma operator (and you don't want to).

I don't understand what you are exactly asking. In all cases, the computer has (sometimes) to do three compares. Why can't you spell them all?

You could perhaps use fancy preprocessor tricks, but in your particular case, you should not.

Of course, the test of an if could be a long expression which takes several lines. It is acceptable and common to write something like

if (a > b
    && a > c
    && a > d
    && somelongandcomplexcondition(a,b,c)
    && a*a > 34)

Notice that Ruslan's answer is computing the max. You might not want to, in particular if b and c are long and complex sub-expressions with side effects.

Think for example of a test like if (c > 1 && a > 1 && a > b && a > b/c); you rely on the lazy "and then" evaluation of && to avoid dividing by zero.

If you want to learn more about C11, see some C reference and refer to its standard n1570.

If you want to learn more about C++11, see some C++ reference and refer to its standard n3337 (or some other, younger standard like C++17).

Both C and C++ define precisely what an if statement can be.

Don't confuse C and C++, they are different programming languages, and both are specified in their standard specification. Some compilers, notably GCC, provide extensions to them. It is your responsibility to decide to use compiler-specific extensions or to stick to the language standard (and hope that your code would be compilable by many compilers respecting that standard).

In some particular cases, your tests (or your C or C++ code) are so long and so repetitive that you could consider some metaprogramming approach: you'll then write some script (or some metaprogram) to emit C (or C++) code in a file (and you could later compile that generated file, or #include it, etc.). There are several examples of C or C++ code generators (e.g. bison, SWIG, etc...) that might inspire you. You could also use some generic preprocessor like GPP or m4, or have your awk or python script (or your other C++ program) generate some C or C++ file, etc... Of course, you will configure your build automation (e.g. your Makefile) for such particular cases.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
-1

One way to make it simpler is to put the values into an array and sort them. Since you didn't give a concrete example, I can't say whether it would help for your case, but when you're dealing with a sorted array, you can do things like:

int i = 0;
while ((a < array[i]) && (i < kMaxElements))
{
    i++;
}

At the end, i will either be equal to kMaxElements, or it will be the index of the item that is equal to or greater than a.

Better yet, you can do a binary search the array to find a specific element. In C++ there are specific algorithms for this in the <algorithm> tools. See specifically, binary_search(), lower_bound(), and upper_bound().

user1118321
  • 25,567
  • 4
  • 55
  • 86
  • Uhm sorting just to find the max isn't such a great idea... It's O(n log n) instead of O(n). The binary search idea is dubious as well - why look for the index in log(n) time if all you need to do is to check the last two elements in O(1) time? – Matteo Italia Jan 14 '19 at 06:13
  • The point is that the OP didn't specify what they're trying to do, and it looks like they're trying to figure out where in a set of numbers a particular value falls. If the data is sorted that's easier to do. The data may already be sorted so it doesn't need to be sorted via an O(n log n) algorithm. We don't know since no context was given. In the context of already sorted data, searching may get a useful result more quickly than comparing against every value. That's all I'm trying to say here. – user1118321 Jan 14 '19 at 06:17