给定一组字符串,比如 "String1", "String2",..., "StringN"
,C++ 中确定(返回 true
或 false
) 给定的 string s
是否匹配上述集合中的任何字符串?
Boost.Regex 可以用于此任务吗?
最佳答案
std::unordered_set
将提供最有效的查找(摊销常数时间)。
#include <unordered_set>
#include <string>
#include <cassert>
int main() {
std::unordered_set<std::string> s = {"Hello", "Goodbye", "Good morning"};
assert(s.find("Goodbye") != s.end());
assert(s.find("Good afternoon") == s.end());
return 0;
}
关于c++ - 检查给定字符串是否等效于给定字符串集中至少一个字符串的有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20791550/