std::boyer_moore_horspool_searcher
定义于头文件 <functional> |
||
template< class RandomIt1, class Hash = std::hash<typename std::iterator_traits<RandomIt1>::value_type>, |
(since C++17) | |
一种适用于 Searcher 重载的 std::search 的搜索器,它实现了 Boyer-Moore-Horspool 字符串搜索算法。
std::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, |
||
通过存储 pat_first、pat_last、hf 和 pred 的副本,并设置任何必要的内部数据结构,来构造 std::boyer_moore_horspool_searcher
。
RandomIt1
的值类型必须是 DefaultConstructible、CopyConstructible 和 CopyAssignable。
对于类型为 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)。
[编辑] 注解
特性测试 宏 | 值 | 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++17) |
标准 C++ 库搜索算法实现 (类模板) |
(C++17) |
Boyer-Moore 搜索算法实现 (类模板) |