Concise way to say equal to set of values in C++

后端 未结 5 672
后悔当初
后悔当初 2021-01-23 10:35

For example I have the following string,

if (str[i] == \'(\' ||
    str[i] == \')\' ||
    str[i] == \'+\' ||
    str[i] == \'-\' ||
    str[i] == \'/\' ||
    s         


        
5条回答
  •  萌比男神i
    2021-01-23 11:15

    Maybe not "more concise", but I think this style is succinct and expressive at the point of the test.

    Of course is_arithmetic_punctuation needn't be a lambda if you're going to use it more than once. It could be a function or a function object.

    auto is_arithmetic_punctuation = [](char c)
    {
      switch(c)
      {
          case '(':
          case ')':
          case '+':
          case '-':
          case '/':
          case '*':
              return true;
          default:
              return false;
      }
    };
    
    if (is_arithmetic_punctuation(str[i]))
    {
      // ...
    }
    

提交回复
热议问题