命名空间
变体
操作

std::default_searcher

来自 cppreference.cn
< cpp‎ | utility‎ | functional
 
 
 
函数对象
函数调用
(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_searcherCopyConstructibleCopyAssignable

内容

[编辑] 成员函数

std::default_searcher::default_searcher

default_searcher( ForwardIt pat_first,

                  ForwardIt pat_last,

                  BinaryPredicate pred = BinaryPredicate() );
(自 C++17 起)
(constexpr 自 C++20 起)

通过存储 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 起)
(constexpr 自 C++20 起)

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) 相等的子序列的第一个位置和最后一个位置之后的位置;否则为一对 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 搜索算法实现
(类模板) [编辑]