Namespaces
Variants

std:: default_searcher

From cppreference.net
Utilities library
Function objects
Function invocation
(C++17) (C++23)
Identity function object
(C++20)
Old binders and adaptors
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
( until C++17* ) ( until C++17* )
( until C++17* ) ( until C++17* )

( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until 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 可复制构造 可复制赋值 的。

目录

成员函数

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 搜索算法实现
(类模板)