给定一组字符串,比如说"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; }