Determine if a string contains only alphanumeric characters (or a space)

后端 未结 4 885
遇见更好的自我
遇见更好的自我 2020-12-14 00:49

I am writing a function that determines whether a string contains only alphanumeric characters and spaces. I am effectively testing whether it matches the regular expression

4条回答
  •  猫巷女王i
    2020-12-14 01:36

    You can also do this with binders so you can drop the helper function. I'd recommend Boost Binders as they are much easier to use then the standard library binders:

    bool string_is_valid(const std::string &str)
    {
        return find_if(str.begin(), str.end(),
            !boost::bind(isalnum, _1) || boost::bind(std::not_equal_to, _1, ' ')) == str.end();
    }
    

提交回复
热议问题