C++ multiple strings inside an if statement

前端 未结 5 825
北恋
北恋 2020-12-02 02:26

I\'m having an issue trying to check against multiple possibilities in an if statement.

The user inputs a string, and then I check that string against multiple possi

5条回答
  •  鱼传尺愫
    2020-12-02 02:53

    Using std::set and c++11 you can do one liner with similar syntax as your.

    check this:

    #include 
    #include 
    #include 
    
    int main()
    {
    
      if( (std::set{"Seven", "seven", "7"}).count("seven") )
      {
          std::cout << "foo\n";
      }
    
      std::string theString("6");
    
      if( (std::set{"Six", "six", "6"}).count(theString) )
      {
          std::cout << "bar\n";
      }
    }
    

提交回复
热议问题