命名空间
变体
操作

std::default_searcher

来自 cppreference.cn
 
 
 
函数对象
函数调用
(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)
搜索器
default_searcher
(C++17)
旧绑定器和适配器
(直到 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() );
(C++17 起)
(C++20 起为 constexpr)

通过存储 pat_firstpat_lastpred 的副本构造一个 std::default_searcher

参数

pat_first, pat_last - 一对迭代器,指示要搜索的字符串
pred - 一个可调用对象,用于确定相等性

异常

BinaryPredicateForwardIt 的复制构造函数抛出的任何异常。

std::default_searcher::operator()

template< class ForwardIt2 >

std::pair<ForwardIt2, ForwardIt2>

    operator()( ForwardIt2 first, ForwardIt2 last ) const;
(C++17 起)
(C++20 起为 constexpr)

std::search 的 Searcher 重载调用的成员函数,用于使用此搜索器执行搜索。

返回一对迭代器 i, j,其中 istd::search(first, last, pat_first, pat_last, pred)jstd::next(i, std::distance(pat_first, pat_last)),除非 std::search 返回 last (未匹配),在这种情况下 j 也等于 last

参数

first, last - 一对迭代器,指示要检查的字符串

返回值

一对迭代器,指向 [firstlast) 中第一个和最后一个位置(之后一个)的子序列,该子序列与 [pat_firstpat_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 搜索算法实现
(类模板) [编辑]