Namespaces
Variants

std::default_searcher

来自cppreference.net
 
 
 
函数对象
函数调用
(C++17)(C++23)
恒等函数对象
(C++20)
透明运算符包装器
(C++14)
(C++14)
(C++14)
(C++14)  
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)

旧的绑定器和适配器
(直到 C++17*)
(直到 C++17*)
(直到 C++17*)
(直到 C++17*)  
(直到 C++17*)
(直到 C++17*)(直到 C++17*)(直到 C++17*)(直到 C++17*)
(直到 C++20*)
(直到 C++20*)
(直到 C++17*)(直到 C++17*)
(直到 C++17*)(直到 C++17*)

(直到 C++17*)
(直到 C++17*)(直到 C++17*)(直到 C++17*)(直到 C++17*)
(直到 C++20*)
(直到 C++20*)
 
定义于头文件<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,

BinaryPredicate pred = BinaryPredicate ( ) ) ;
(since C++17)
(constexpr since 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 >

operator ( ) ( ForwardIt2 first, ForwardIt2 last ) const ;
(since C++17)
(constexpr since 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 ) 通过 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 << "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

参阅

搜索元素范围的首次出现
(函数模板 & 算法函数对象)
Boyer-Moore 搜索算法实现
(类模板)
Boyer-Moore-Horspool 搜索算法实现
(类模板)