名稱空間
變體
操作

std::boyer_moore_horspool_searcher

來自 cppreference.com
 
 
 
函式物件
函式呼叫
(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)
搜尋器
boyer_moore_horspool_searcher
(C++17)    
舊繫結器和介面卡
(直到 C++17*)
(直到 C++17*)
(直到 C++17*)
(直到 C++17*)  
(直到 C++17*)
(until C++17*)(until C++17*)(until C++17*)(until C++17*)
(直到 C++20*)
(直到 C++20*)
(直到 C++17*)(直到 C++17*)
(直到 C++17*)(直到 C++17*)

(直到 C++17*)
(until C++17*)(until C++17*)(until C++17*)(until C++17*)
(直到 C++20*)
(直到 C++20*)
 
定義於標頭檔案 <functional>
template< class RandomIt1,

          class Hash = std::hash<typename std::iterator_traits<RandomIt1>::value_type>,
          class BinaryPredicate = std::equal_to<> >

class boyer_moore_horspool_searcher;
(C++17 起)

一個用於 std::searchSearcher 過載的搜尋器,它實現了 Boyer-Moore-Horspool 字串搜尋演算法

std::boyer_moore_horspool_searcher可複製構造的(CopyConstructible)可複製賦值的(CopyAssignable)

RandomIt1 必須滿足 舊式隨機訪問迭代器(LegacyRandomAccessIterator)的要求。

目錄

[編輯] 成員函式

std::boyer_moore_horspool_searcher::boyer_moore_horspool_searcher

boyer_moore_horspool_searcher( RandomIt1 pat_first,

                               RandomIt1 pat_last,
                               Hash hf = Hash(),

                               BinaryPredicate pred = BinaryPredicate() );

透過儲存 pat_firstpat_lasthfpred 的副本,並設定任何必要的內部資料結構來構造一個 std::boyer_moore_horspool_searcher

RandomIt1 的值型別必須是 可預設構造的(DefaultConstructible)可複製構造的(CopyConstructible)可複製賦值的(CopyAssignable)

對於型別為 std::iterator_traits<RandomIt1>::value_type 的任意兩個值 AB,如果 pred(A, B) == true,則 hf(A) == hf(B) 應為 true

引數

pat_first, pat_last - 一對迭代器,指示要搜尋的字串
hf - 一個可呼叫物件,用於對字串元素進行雜湊
pred - 一個可呼叫物件,用於確定相等性

異常

由以下任一情況丟擲的任何異常:

  • RandomIt1 的複製建構函式;
  • RandomIt1 的值型別的預設建構函式、複製建構函式或複製賦值運算子;或
  • BinaryPredicateHash 的複製建構函式或函式呼叫運算子。

如果無法為內部資料結構分配所需的額外記憶體,也可能丟擲 std::bad_alloc

std::boyer_moore_horspool_searcher::operator()

template< class RandomIt2 >
std::pair<RandomIt2, RandomIt2> operator()( RandomIt2 first, RandomIt2 last ) const;

std::search 的 Searcher 過載呼叫的成員函式,用於使用此搜尋器執行搜尋。RandomIt2 必須滿足 舊式隨機訪問迭代器(LegacyRandomAccessIterator)的要求。

RandomIt1RandomIt2 必須具有相同的值型別。

引數

first, last - 一對迭代器,指示要檢查的字串

返回值

如果模式 [pat_firstpat_last) 為空,則返回 std::make_pair(first, first)

否則,返回一對迭代器,指向 [firstlast) 中與 [pat_firstpat_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 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::boyer_moore_horspool_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++ 庫搜尋演算法實現
(類模板) [編輯]
Boyer-Moore 搜尋演算法實現
(類模板) [編輯]