std::experimental::search - cppreference.com
来自cppreference.com
template< class ForwardIterator, class Searcher > ForwardIterator search( ForwardIterator first, ForwardIterator last, const Searcher& searcher ); |
(库基础 TS) | |
在序列 [first, last) 中搜索 searcher 的构造函数所指定的模式。
|
相当于执行 |
(C++17 前) |
|
相当于执行 |
(C++17 起) |
Searcher 不必为可复制构造 (CopyConstructible) 。
标准库提供了下列搜索器:
参数
返回值
返回 searcher.operator() 的结果,即指向找到子字符串的位置的迭代器,或当未找到时返回 last 的副本。
复杂度
取决于搜索器。
示例
#include <experimental/algorithm> #include <experimental/functional> #include <iostream> #include <string> 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::experimental::search(in.begin(), in.end(), std::experimental::make_boyer_moore_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