命名空间
变体
操作

std::boyer_moore_searcher

来自 cppreference.cn
< cpp‎ | utility‎ | functional
 
 
 
函数对象
函数调用
(C++17)(C++23)
恒等函数对象
(C++20)
透明运算符包装器
(C++14)
(C++14)
(C++14)
(C++14)  
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)

求反器
(C++17)
搜索器
boyer_moore_searcher
(C++17)
旧式绑定器与适配器
(直到 C++17*)
(直到 C++17*)
(直到 C++17*)
(直到 C++17*)  
(直到 C++17*)
(直到 C++17*)(直到 C++17*)(直到 C++17*)(直到 C++17*)
(直到 C++20*)
(直到 C++20*)
(直到 C++17*)(直到 C++17*)
(直到 C++17*)(直到 C++17*)

(直到 C++17*)
(直到 C++17*)(直到 C++17*)(直到 C++17*)(直到 C++17*)
(直到 C++20*)
(直到 C++20*)
 
定义于头文件 <functional>
template< class RandomIt1,

          class Hash = std::hash<typename std::iterator_traits<RandomIt1>::value_type>,
          class BinaryPredicate = std::equal_to<> >

class boyer_moore_searcher;
(C++17 起)

一个适用于 Searcher 重载的 std::search 的搜索器,它实现了 Boyer-Moore 字符串搜索算法

std::boyer_moore_searcherCopyConstructibleCopyAssignable

RandomIt1 必须满足 LegacyRandomAccessIterator 的要求。

内容

[edit] 成员函数

std::boyer_moore_searcher::boyer_moore_searcher

boyer_moore_searcher( RandomIt1 pat_first,

                      RandomIt1 pat_last,
                      Hash hf = Hash(),

                      BinaryPredicate pred = BinaryPredicate() );

通过存储 pat_firstpat_lasthfpred 的副本,并设置任何必要的内部数据结构,构造一个 std::boyer_moore_searcher

RandomIt1 的值类型必须是 DefaultConstructibleCopyConstructibleCopyAssignable

对于类型为 std::iterator_traits<RandomIt1>::value_type 的任意两个值 AB,如果 pred(A, B) == true,则 hf(A) == hf(B) 应为 true

参数

pat_first, pat_last - 一对迭代器,指示要搜索的字符串
hf - 一个可调用对象,用于哈希字符串的元素
pred - 一个可调用对象,用于确定相等性

异常

由以下项抛出的任何异常:

  • RandomIt1 的复制构造函数;
  • RandomIt1 的值类型的默认构造函数、复制构造函数和复制赋值运算符;或
  • BinaryPredicateHash 的复制构造函数和函数调用运算符。

如果无法分配内部数据结构所需的额外内存,也可能抛出 std::bad_alloc

std::boyer_moore_searcher::operator()

template< class RandomIt2 >
std::pair<RandomIt2, RandomIt2> operator()( RandomIt2 first, RandomIt2 last ) const;
(C++17 起)

std::search 的 Searcher 重载调用的成员函数,以使用此搜索器执行搜索。RandomIt2 必须满足 LegacyRandomAccessIterator 的要求。

RandomIt1RandomIt2 必须具有相同的值类型。

参数

first, last - 一对迭代器,指示要检查的字符串

返回值

如果模式 [pat_firstpat_last) 为空,则返回 std::make_pair(first, first)

否则,返回一对迭代器,指向 [firstlast) 中第一个和最后一个位置之后的位置,其中找到了一个与 [pat_firstpat_last) 相等的子序列(由 pred 定义),否则返回 std::make_pair(last, last)

[edit] 注释

特性测试 Std 特性
__cpp_lib_boyer_moore_searcher 201603L (C++17) searchers

[edit] 示例

#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <string_view>
 
int main()
{
    constexpr std::string_view haystack =
        "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed "
        "do eiusmod tempor incididunt ut labore et dolore magna aliqua";
 
    const std::string_view needle{"pisci"};
 
    if (const auto it = std::search(haystack.begin(), haystack.end(),
            std::boyer_moore_searcher(needle.begin(), needle.end()));
        it != haystack.end()
    )
        std::cout << "The string " << std::quoted(needle) << " found at offset "
                  << it - haystack.begin() << '\n';
    else
        std::cout << "The string " << std::quoted(needle) << " not found\n";
}

输出

The string "pisci" found at offset 43

[edit] 参见

搜索元素范围的首次出现
(函数模板) [edit]
标准 C++ 库搜索算法实现
(类模板) [edit]
Boyer-Moore-Horspool 搜索算法实现
(类模板) [edit]