std::default_searcher
来自 cppreference.cn
< cpp | utility | functional
定义于头文件 <functional> |
||
template< class ForwardIt, class BinaryPredicate = std::equal_to<> > class default_searcher; |
(自 C++17 起) | |
一个适用于 Searcher 重载的 std::search 的类,它将搜索操作委托给 C++17 之前的标准库的 std::search。
std::default_searcher
是 CopyConstructible 和 CopyAssignable。
内容 |
[编辑] 成员函数
std::default_searcher::default_searcher
default_searcher( ForwardIt pat_first, ForwardIt pat_last, |
(自 C++17 起) (constexpr 自 C++20 起) |
|
通过存储 pat_first、pat_last 和 pred 的副本,构造一个 std::default_searcher
。
参数
pat_first, pat_last | - | 一对迭代器,指定要搜索的字符串 |
pred | - | 用于确定相等性的可调用对象 |
异常
BinaryPredicate
或 ForwardIt
的复制构造函数可能抛出的任何异常。
std::default_searcher::operator()
template< class ForwardIt2 > std::pair<ForwardIt2, ForwardIt2> |
(自 C++17 起) (constexpr 自 C++20 起) |
|
由 std::search 的 Searcher 重载调用的成员函数,以使用此搜索器执行搜索。
返回一对迭代器 i, j
,其中 i
是 std::search(first, last, pat_first, pat_last, pred),j
是 std::next(i, std::distance(pat_first, pat_last)),除非 std::search
返回 last(未找到匹配项),在这种情况下,j
也等于 last。
参数
first, last | - | 一对迭代器,指定要检查的字符串 |
返回值
一对迭代器,指向 [
first,
last)
中找到的与 [
pat_first,
pat_last)
相等的子序列的第一个位置和最后一个位置之后的位置;否则为一对 last 的副本。
[编辑] 示例
运行此代码
#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::default_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) |
Boyer-Moore 搜索算法实现 (类模板) |
Boyer-Moore-Horspool 搜索算法实现 (类模板) |