我正在编写一个函数来确定字符串是否只包含字母数字字符和空格.我正在有效地测试它是否与正则表达式匹配^[[:alnum:] ]+$但不使用正则表达式.这是我到目前为止:
#include <algorithm>
static inline bool is_not_alnum_space(char c)
{
return !(isalpha(c) || isdigit(c) || (c == ' '));
}
bool string_is_valid(const std::string &str)
{
return find_if(str.begin(), str.end(), is_not_alnum_space) == str.end();
}
Run Code Online (Sandbox Code Playgroud)
是否有更好的解决方案或"更多C++"方式来做到这一点?