I need to write a condition that checks if an enum variable in range of values, like it can be done in E language:
enum EnumVariable {a, b, d, g, f, t, k, i}; if (EnumVariable in [ a, g, t, i]) { ... }
Is there a better way in C++ than ask 4 times if EnumVariable==a or EnumVariable==b
etc.?
The equality operator (==) is used to compare two values or expressions. It is used to compare numbers, strings, Boolean values, variables, objects, arrays, or functions. The result is TRUE if the expressions are equal and FALSE otherwise.
It appeared, that several people liked my comment and asked to post it as an answer, so:
You can actually use switch
for integral types without break
-s between some values.
For example, your code could look like:
enum EnumVariable {a, b, d, g, f, t, k, i}; switch( EnumVariable ) { case a: case g: case t: case i: // do something break; // handler other values };
This would be very efficient, but would work for integral types only, and not for std::string
, for example. For other types, you may try to use some of the other answers' suggestions.
It's a bit long for writing, but very, very efficient and does what you need.
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