【问题标题】:What algorithm is used in std::search?std::search 中使用什么算法?
【发布时间】:2012-02-27 15:42:15
【问题描述】:

有许多字符串匹配算法可用于在大文本中查找模式(字符串),如 Boyer-Moore、Aho-Corasick 等。

使用哪种字符串匹配算法在C++中实现std::search函数?

【问题讨论】:

  • @Mr.Anubis- 我认为 Adban 是在谈论 std::search 算法,它在另一个任意序列中搜索任意序列。
  • @templatetypedef 抱歉,我只是想为什么不使用专门为字符串制作的std::string::find

标签: c++ string algorithm pattern-matching string-matching


【解决方案1】:

根据 C++03 ISO 标准,§25.1.9/3,std::search 的复杂度为

复杂度:最多 (last1 - first1) * (last2 - first2) 应用对应谓词

这是实现该算法的唯一要求。 ISO 规范没有指定这里应该使用哪种算法,它完全依赖于实现。这些时间界限允许使用朴素的序列搜索算法Knuth-Morris-PrattBoyer-MooreRabin-Karp。要知道正在使用哪个,您可能应该提取您正在使用的标准库版本的文档。也就是说,你不能指望它是便携的。我的猜测是大多数实现只使用朴素匹配算法,因为在实践中通常不会出现最坏的情况。

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2012-11-11
    • 2014-05-18
    • 2011-07-19
    • 2010-11-29
    • 1970-01-01
    • 2013-01-10
    • 2019-05-15
    • 2021-11-11
    • 2021-01-03
    相关资源
    最近更新 更多