How to simplify checking if a pair of numbers is (1,2) or (2,1)?

This is the most direct way to make this comparison.

It is also more readable than any alternative you may come up with, so no need to change it.


Here's one way to write it that I think is more readable:

if (std::set{x,y} == std::set{1,2})
{
    return 1;
}

Note that this is not as efficient as the version in your question. Don't write this in code that you're going to ship as a library, for example. But this is perfectly reasonable to write in your own programs when performance is not a concern.

Here's a demo.

Tags:

C++

C

Logic