命名空间
变体
操作

std::boyer_moore_horspool_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_horspool_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_horspool_searcher;
(since C++17)

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

std::boyer_moore_horspool_searcherCopyConstructibleCopyAssignable

RandomIt1 必须满足 LegacyRandomAccessIterator 的要求。

目录

[编辑] 成员函数

std::boyer_moore_horspool_searcher::boyer_moore_horspool_searcher

boyer_moore_horspool_searcher( RandomIt1 pat_first,

                               RandomIt1 pat_last,
                               Hash hf = Hash(),

                               BinaryPredicate pred = BinaryPredicate() );

通过存储 pat_firstpat_lasthfpred 的副本,并设置任何必要的内部数据结构,来构造 std::boyer_moore_horspool_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_horspool_searcher::operator()

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

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)

[编辑] 注解

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

[编辑] 示例

#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <string_view>
 
int main()
{
    constexpr std::string_view in =
        "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"};
 
    auto it = std::search(in.begin(), in.end(),
                  std::boyer_moore_horspool_searcher(
                      needle.begin(), needle.end()));
    if (it != in.end())
        std::cout << "The string " << std::quoted(needle) << " found at offset "
                  << it - in.begin() << '\n';
    else
        std::cout << "The string " << std::quoted(needle) << " not found\n";
}

输出

The string "pisci" found at offset 43

[编辑] 参见

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