C++ 参考手册

位置:首页 > C++ 参考手册 >工具库 >函数对象 > std::boyer_moore_horspool_searcher

定义于头文件 <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 起)

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

boyer_moore_horspool_searcher可复制构造 (CopyConstructible) 可复制赋值 (CopyAssignable)

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 的副本构造一个 boyer_moore_horspool_searcher ,安装任何需要的内部数据结构。

RandomIt1 的值必须是可默认构造 (DefaultConstructible) 可复制构造 (CopyConstructible) 可复制赋值 (CopyAssignable)

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

参数

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

异常

任何会为下列者抛出的异常

  • RandomIt1 的复制构造函数;
  • RandomIt1 的 value_type 的默认构造函数、复制构造函数或复制赋值运算符;或
  • 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_first, pat_last) )为空,则返回 make_pair(first, first)

否则,返回于 [first, last) 中指向首及尾后一位的一对迭代器,其中定位了与 [pat_first, pat_last)pred 所定义比较相等的子序列,否则返回 make_pair(last, last)

示例

#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
 
int main()
{
    std::string in = "Lorem ipsum dolor sit amet, consectetur adipiscing elit,"
                     " sed do eiusmod tempor incididunt ut labore et dolore magna aliqua";
    std::string 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 " << needle << " found at offset "
                  << it - in.begin() << '\n';
    else
        std::cout << "The string " << needle << " not found\n";
}

输出:

The string pisci found at offset 43

参阅

搜索一个元素范围
(函数模板)