Checking if a value is within a range in if statment

后端 未结 2 534
忘了有多久
忘了有多久 2020-12-21 20:15

I did below:

if( \'0\' <= infix.at(i)<= \'9\'){
// some logic
}

Compiler gave me warning unsafe use of type \'bool\' in operation. I

2条回答
  •  情话喂你
    2020-12-21 20:31

    The first version would be evaluated like this:

    if( ( '0' <= infix.at(i) ) <= '9' )
    

    which is why you get a warning about bool - you wind up with true/false <= '9' which doesn't make sense.

    Some languages allow you to chain comparison operators, but C++ is not one of them.

提交回复
热议问题