Namespaces
Variants

std::experimental:: search

From cppreference.net
定义于头文件 <experimental/algorithm>
template < class ForwardIterator, class Searcher >

ForwardIterator search ( ForwardIterator first, ForwardIterator last,

const Searcher & searcher ) ;
(库基础 TS)

在序列 [ first , last ) 中搜索 searcher 构造函数中指定的模式。

等效执行 searcher ( first, last )

(C++17 前)

等效执行 searcher ( first, last ) . first

(C++17 起)

Searcher 无需是 CopyConstructible

标准库提供以下搜索器:

标准 C++ 库搜索算法实现
(类模板)
Boyer-Moore 搜索算法实现
(类模板)
Boyer-Moore-Horspool 搜索算法实现
(类模板)

目录

参数

返回值

返回 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

参见

在元素范围中搜索首个匹配序列
(函数模板)