我正在写一个方法,其签名是
bool isValidString(std::string value)
在这个方法中,我想搜索value
中的所有字符都属于一组字符,这是一个常量字符串
const std::string ValidCharacters("abcd")
为了执行此搜索,我从 value
中取出一个字符并在 ValidCharacters
中搜索,如果此检查失败则它是无效字符串 STL 库中是否还有其他替代方法做这个检查。
最佳答案
bool isValidString(const std::string& s) {
return std::string::npos == s.find_first_not_of("abcd");
}
关于c++ - 字符串包含有效字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3211678/