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 CopyConstructible かつ CopyAssignable です。

目次

メンバー関数

std::default_searcher:: default_searcher

default_searcher ( ForwardIt pat_first,

ForwardIt pat_last,

BinaryPredicate pred = BinaryPredicate ( ) ) ;
(C++17以降)
(C++20以降constexpr)

std::default_searcher を構築し、 pat_first pat_last 、および pred のコピーを格納します。

パラメータ

pat_first, pat_last - 検索対象の文字列を指定するイテレータのペア
pred - 等価性を判定するために使用される呼び出し可能オブジェクト

例外

BinaryPredicate または ForwardIt のコピーコンストラクタによってスローされるあらゆる例外。

std::default_searcher:: operator()

template < class ForwardIt2 >

std:: pair < ForwardIt2, ForwardIt2 >

operator ( ) ( ForwardIt2 first, ForwardIt2 last ) const ;
(C++17以降)
(C++20以降 constexpr)

Searcherオーバーロードの std::search によって呼び出され、このサーチャーで検索を実行するメンバー関数。

イテレータのペア 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検索アルゴリズムの実装
(クラステンプレート)