std::default_searcher
來自 cppreference.com
定義於標頭檔案 <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, |
(C++17 起) (C++20 起為 constexpr) |
|
透過儲存 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> |
(C++17 起) (C++20 起為 constexpr) |
|
由 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
[編輯] 參閱
搜尋一個範圍的元素首次出現的位置 (函式模板) | |
(C++17) |
Boyer-Moore 搜尋演算法實現 (類模板) |
Boyer-Moore-Horspool 搜尋演算法實現 (類模板) |