std::default_searcher
来自cppreference.com
< cpp | utility | functional
| 在标头 <functional> 定义
|
||
| template< class ForwardIt, class BinaryPredicate = std::equal_to<> > class default_searcher; |
(C++17 起) | |
适合用于 std::search 的搜索器 (Searcher) 重载的类,它将搜索操作委派到 C++17 前标准库的 std::search。
default_searcher 可复制构造 (CopyConstructible) 且可复制赋值 (CopyAssignable) 。
成员函数
std::default_searcher::default_searcher
default_searcher( ForwardIt pat_first, ForwardIt pat_last, |
(C++17 起) (C++20 起为 constexpr) |
|
通过存储 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 起) (C++20 起为 constexpr) |
|
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) 按 pred 的定义比较相等的子序列的首及尾后一位,否则返回一对 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 << "字符串 " << std::quoted(needle) << " 在偏移 " << it - in.begin() << " 处找到\n"; else std::cout << "字符串 " << std::quoted(needle) << " 未找到\n"; }
输出:
字符串 "pisci" 在偏移 43 处找到
参阅
| 搜索一个元素范围的首次出现 (函数模板) | |
| (C++17) |
Boyer-Moore 搜索算法实现 (类模板) |
| Boyer-Moore-Horspool 搜索算法实现 (类模板) |