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

标签 c++ regex string string-matching

给定一组字符串,比如 "String1", "String2",..., "StringN",C++ 中确定(返回 truefalse) 给定的 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/

相关文章:

c++ - 表达式无效的空指针

c# - .NET 真的使用 NFA 作为正则表达式引擎吗?

c++ - Profinet/Profibus linux

c++ - MFC新手: how to determine if a character is hexadecimal using "FindOneOf()"

c++ - QList<double> in QT with Visual Studio 添加

javascript - 无论有效的测试值如何,正则表达式始终返回始终为真或始终为假

regex - Scala 中的递归正则表达式

Python:删除包括一行中某个字符之后的所有内容

swift - 为什么 string.append(character) 编译失败,错误为 "cannot assign value of type ' ( )' to type ' String' "?

python - numpy.memmap 用于字符串数组?