r/cpp_questions • u/Big-Apartment6872 • 11h ago
OPEN Are there any other ways to check if a char is equal to different values?
I'm working on some homework for class where I'm checking if a character is equal to: '+', '-', '/', or '*' for this postfix to infix converter.
I know I'm able to do this in two ways I can think of with:
// 1.
if (c == '+' || c == '-' || c == '/' || c == '*') {
// Code
}
// or 2.
bool isOperator(const char c) {
return (c == '+' || c == '-' || c== '/' || c == '*');
}
...
if (isOperator(c)) {
// Code
}
But is there another way to do this that's shorter? Maybe something along the lines of this?
if (c == {'+', '-', '/', '*'}) {
// Code
}
I looked on cpp reference and stack overflow but I wasn't able to find an answer. And to be fair option 1 and 2 work just find, I'm just curious if there are other methods out there that I'm not familiar with. Thanks!