std::default_searcher
From cppreference.com
< cpp | utility | functional
| ヘッダ <functional> で定義 |
||
| template< class ForwardIt, class BinaryPredicate = std::equal_to<> > class default_searcher; |
(C++17以降) | |
Searcher のオーバーロードで使用するのに適したクラスで、検索操作を C++17 より前の標準ライブラリの std::search に委譲します。
std::default_searcher は CopyConstructible および CopyAssignable です。
目次 |
[edit] メンバ関数
std::default_searcher::default_searcher
default_searcher( ForwardIt pat_first, ForwardIt pat_last, |
(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> |
(C++17以降) (C++20 以降 constexpr) |
|
std::search の Searcher オーバーロードによって、この 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 | - | 検査対象の文字列を指定するイテレータのペア |
戻り値
pred で定義されるように、[pat_first, pat_last) と比較して等しい部分シーケンスが見つかった、[first, last) の最初の位置と最後の一つ後の位置を指すイテレータのペア。それ以外の場合は 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] 関連項目
| 要素の範囲の最初の出現を検索する (関数テンプレート) | |
| (C++17) |
ボイヤー・ムーア法探索アルゴリズム実装 (クラステンプレート) |
| ボイヤー・ムーア・ホースプール法探索アルゴリズム実装 (クラステンプレート) |