检查给定字符串是否等于给定字符串集中至少一个字符串的有效方法

Ole*_*ikh 4 c++ regex string string-matching

给定一组字符串,比如说"String1", "String2",..., "StringN",C++中确定(返回truefalse)是否string s与上述集合中的任何字符串匹配的最有效方法是什么?

可以将Boost.Regex用于此任务吗?

Jos*_*eld 9

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;
}
Run Code Online (Sandbox Code Playgroud)