Check for multiple values when using comparison operators

后端 未结 4 1331

I\'ve always been under the impression that for any comparison statement, i.e. X == Y or X != Y is the format, and you chain statements together with <

4条回答
  •  梦毁少年i
    2021-02-19 16:48

    #include 
    #include 
    #include 
    #include 
    #include 
     
    template
    bool is_one_of(const Type& needle, const Next& next)
    {return needle==next;}
    template
    bool is_one_of(const Type& needle, const Next& next, Rest... haystack)
    {return needle==next || is_one_of(needle, haystack...);}
     
    int main() {
        std::string X, Y;
        if (is_one_of(X, Y, "HI"))
            std::cout << "it is!";
        else
            std::cout << "it isn't!";
        return 0;
    }
    

    proof of compilation. Xeo also observes that std::any_of, std::all_of and std::none_of may have been useful, depending on your actual needs and desires.

提交回复
热议问题