std::boyer_moore_searcher
定義於標頭檔案 <functional> |
||
template< class RandomIt1, class Hash = std::hash<typename std::iterator_traits<RandomIt1>::value_type>, |
(C++17 起) | |
一個搜尋器,適用於 std::search 的 Searcher 過載,實現了 Boyer-Moore 字串搜尋演算法。
std::boyer_moore_searcher
是 可複製構造的(CopyConstructible) 和 可複製賦值的(CopyAssignable)。
RandomIt1
必須滿足 LegacyRandomAccessIterator 的要求。
目錄 |
[編輯] 成員函式
std::boyer_moore_searcher::boyer_moore_searcher
boyer_moore_searcher( RandomIt1 pat_first, RandomIt1 pat_last, |
||
透過儲存 pat_first、pat_last、hf 和 pred 的副本,並設定任何必要的內部資料結構,構造一個 std::boyer_moore_searcher
。
RandomIt1
的值型別必須是 可預設構造的(DefaultConstructible)、可複製構造的(CopyConstructible) 和 可複製賦值的(CopyAssignable)。
對於型別 std::iterator_traits<RandomIt1>::value_type 的任意兩個值 A
和 B
,如果 pred(A, B) == true,則 hf(A) == hf(B) 應當為 true。
引數
pat_first, pat_last | - | 一對迭代器,指示要搜尋的字串 |
hf | - | 一個可呼叫物件,用於對字串元素進行雜湊 |
pred | - | 一個可呼叫物件,用於確定相等性 |
異常
由以下任一情況丟擲的任何異常:
RandomIt1
的複製建構函式;RandomIt1
的值型別的預設建構函式、複製建構函式和複製賦值運算子;或BinaryPredicate
或Hash
的複製建構函式和函式呼叫運算子。
如果無法為內部資料結構分配所需的額外記憶體,也可能丟擲 std::bad_alloc。
std::boyer_moore_searcher::operator()
template< class RandomIt2 > std::pair<RandomIt2, RandomIt2> operator()( RandomIt2 first, RandomIt2 last ) const; |
(C++17 起) | |
由 std::search 的 Searcher 過載呼叫以使用此搜尋器執行搜尋的成員函式。RandomIt2
必須滿足 LegacyRandomAccessIterator 的要求。
RandomIt1
和 RandomIt2
必須具有相同的值型別。
引數
first, last | - | 一對迭代器,指示要檢查的字串 |
返回值
如果模式 [
pat_first,
pat_last)
為空,則返回 std::make_pair(first, first)。
否則,返回一對迭代器,指向 [
first,
last)
中與 [
pat_first,
pat_last)
透過 pred 定義的比較相等的子序列的起始位置和末尾後一個位置,否則返回 std::make_pair(last, last)。
[編輯] 注意
特性測試宏 | 值 | 標準 | 特性 |
---|---|---|---|
__cpp_lib_boyer_moore_searcher |
201603L |
(C++17) | 搜尋器 |
[編輯] 示例
#include <algorithm> #include <functional> #include <iomanip> #include <iostream> #include <string_view> int main() { constexpr std::string_view haystack = "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"}; if (const auto it = std::search(haystack.begin(), haystack.end(), std::boyer_moore_searcher(needle.begin(), needle.end())); it != haystack.end() ) std::cout << "The string " << std::quoted(needle) << " found at offset " << it - haystack.begin() << '\n'; else std::cout << "The string " << std::quoted(needle) << " not found\n"; }
輸出
The string "pisci" found at offset 43
[編輯] 參閱
搜尋一個範圍的元素首次出現的位置 (函式模板) | |
(C++17) |
標準 C++ 庫搜尋演算法實現 (類模板) |
Boyer-Moore-Horspool 搜尋演算法實現 (類模板) |