名稱空間
變體
操作

std::experimental::boyer_moore_searcher, std::experimental::make_boyer_moore_searcher

來自 cppreference.com
< cpp‎ | 實驗性
 
 
實驗性
技術規範
檔案系統庫 (檔案系統 TS)
庫基礎 (庫基礎 TS)
庫基礎 2 (庫基礎 TS v2)
庫基礎 3 (庫基礎 TS v3)
並行性擴充套件 (並行性 TS)
並行性擴充套件 2 (並行性 TS v2)
併發性擴充套件 (併發性 TS)
併發擴充套件 2 (併發 TS v2)
概念 (概念 TS)
範圍 (範圍 TS)
反射 (反射 TS)
數學特殊函式 (特殊函式 TR)
實驗性非 TS
模式匹配
線性代數
std::execution
契約
2D 圖形
 
 
定義於標頭檔案 <experimental/functional>
template< class RandomIt1,

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

class boyer_moore_searcher;
(庫基礎 TS)

一個適用於 std::experimental::search 的搜尋器,實現了 Boyer-Moore 字串搜尋演算法

boyer_moore_searcher可複製構造的(CopyConstructible)可複製賦值的(CopyAssignable)

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

目錄

[編輯] 成員函式

std::experimental::boyer_moore_searcher::boyer_moore_searcher

boyer_moore_searcher( RandomIt1 pat_first,

                      RandomIt1 pat_last,
                      Hash hf = Hash(),

                      BinaryPredicate pred = BinaryPredicate() );

透過儲存 pat_firstpat_lasthfpred 的副本,並設定任何必要的內部資料結構來構造一個 boyer_moore_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::experimental::boyer_moore_searcher::operator()

template< class RandomIt2 >
RandomIt2 operator()( RandomIt2 first, RandomIt2 last ) const;
(C++17 前)
template< class RandomIt2 >
std::pair<RandomIt2,RandomIt2> operator()( RandomIt2 first, RandomIt2 last ) const;
(C++17 起)

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

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

引數

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

返回值

如果模式 [pat_firstpat_last) 為空,則返回 first

否則,返回 [firstlast) 中第一個位置的迭代器,在該位置找到了與 [pat_firstpat_last) 經由 pred 定義為相等的子序列,或返回 last 的副本。

(C++17 前)

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

否則,返回一對迭代器,指向 [firstlast) 中第一個位置和最後一個位置之後一個位置,在該位置找到了與 [pat_firstpat_last) 經由 pred 定義為相等的子序列,或返回 make_pair(last, last)

(C++17 起)

[編輯] 輔助函式

template< class RandomIt,

          class Hash = std::hash<typename std::iterator_traits<RandomIt>::value_type>,
          class BinaryPredicate = std::equal_to<> >
boyer_moore_searcher<RandomIt, Hash, BinaryPredicate> make_boyer_moore_searcher(
    RandomIt pat_first,
    RandomIt pat_last,
    Hash hf = Hash(),

    BinaryPredicate pred = BinaryPredicate());
(庫基礎 TS)

使用模板引數推導構造 std::experimental::boyer_moore_searcher 的輔助函式。等價於 return boyer_moore_searcher<RandomIt, Hash, BinaryPredicate>(pat_first, pat_last, hf, pred);

[編輯] 引數

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

[編輯] 返回值

一個用引數 pat_firstpat_lasthfpred 構造的 boyer_moore_searcher

[編輯] 示例

#include <experimental/algorithm>
#include <experimental/functional>
#include <iostream>
#include <string>
 
int main()
{
    std::string in = "Lorem ipsum dolor sit amet, consectetur adipiscing elit,"
                     " sed do eiusmod tempor incididunt ut labore et dolore magna aliqua";
    std::string needle = "pisci";
    auto it = std::experimental::search(in.begin(), in.end(),
                  std::experimental::make_boyer_moore_searcher(
                      needle.begin(), needle.end()));
    if (it != in.end())
        std::cout << "The string " << needle << " found at offset "
                  << it - in.begin() << '\n';
    else
        std::cout << "The string " << needle << " not found\n";
}

輸出

The string pisci found at offset 43

[編輯] 另請參閱

搜尋一個範圍的元素首次出現的位置
(函式模板) [編輯]