名前空間
変種
操作

std::default_searcher

From cppreference.com
< cpp‎ | utility‎ | functional
 
 
ユーティリティライブラリ
言語サポート
型のサポート (基本型、RTTI)
ライブラリ機能検査マクロ (C++20)
プログラムユーティリティ
可変引数関数
コルーチンサポート (C++20)
契約サポート (C++26)
三方比較
(C++20)
(C++20)(C++20)(C++20)  
(C++20)(C++20)(C++20)

汎用ユーティリティ
関係演算子 (C++20で非推奨)
 
関数オブジェクト
関数の呼び出し
(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 のオーバーロードで使用するのに適したクラスで、検索操作を C++17 より前の標準ライブラリの std::search に委譲します。

std::default_searcherCopyConstructible および CopyAssignable です。

目次

[edit] メンバ関数

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_firstpat_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)

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

イテレータのペア i, j を返します。ここで istd::search(first, last, pat_first, pat_last, pred) であり、jstd::next(i, std::distance(pat_first, pat_last)) です。ただし、std::searchlast を返した場合(一致なし)は、jlast と等しくなります。

パラメータ

first, last - 検査対象の文字列を指定するイテレータのペア

戻り値

pred で定義されるように、[pat_firstpat_last) と比較して等しい部分シーケンスが見つかった、[firstlast) の最初の位置と最後の一つ後の位置を指すイテレータのペア。それ以外の場合は last のペアのコピー。

[edit]

#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

[edit] 関連項目

要素の範囲の最初の出現を検索する
(関数テンプレート) [編集]
ボイヤー・ムーア法探索アルゴリズム実装
(クラステンプレート) [編集]
ボイヤー・ムーア・ホースプール法探索アルゴリズム実装
(クラステンプレート) [編集]
English 日本語 中文(简体) 中文(繁體)