Namespaces
Variants

std:: boyer_moore_horspool_searcher

From cppreference.net
Utilities library
Function objects
Function invocation
(C++17) (C++23)
Identity function object
(C++20)
Negators
(C++17)
Searchers
boyer_moore_horspool_searcher
(C++17)
Old binders and adaptors
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
( until C++17* ) ( until C++17* )
( until C++17* ) ( until C++17* )

( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until 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 ;
(C++17 起)

一种适用于 Searcher 重载版本的搜索器,可与 std::search 配合使用,该搜索器实现了 Boyer-Moore-Horspool字符串搜索算法

std::boyer_moore_horspool_searcher 满足 可复制构造 可复制赋值 要求。

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_first pat_last hf pred 的副本,并设置必要的内部数据结构来构造一个 std::boyer_moore_horspool_searcher

RandomIt1 的值类型必须满足 可默认构造 可复制构造 可复制赋值 的要求。

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

参数

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

异常

可能抛出以下异常:

  • RandomIt1 的复制构造函数抛出的异常;
  • RandomIt1 值类型的默认构造函数、复制构造函数或复制赋值运算符抛出的异常;
  • BinaryPredicate Hash 的复制构造函数或函数调用运算符抛出的异常。

如果无法为内部数据结构分配所需内存,可能还会抛出 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 的要求。

RandomIt1 RandomIt2 必须具有相同的值类型。

参数

first, last - 指定待搜索字符串的一对迭代器

返回值

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

否则,返回指向 [ first , last ) 范围内与 [ pat_first , pat_last ) pred 定义相等的子序列的首尾迭代器对。若未找到,则返回 std:: make_pair ( last, last )

注释

功能测试 标准 功能
__cpp_lib_boyer_moore_searcher 201603L (C++17) 搜索器

示例

#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搜索算法实现
(类模板)