名稱空間
變體
操作

std::default_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)
搜尋器
default_searcher
(C++17)
舊繫結器和介面卡
(直到 C++17*)
(直到 C++17*)
(直到 C++17*)
(直到 C++17*)  
(直到 C++17*)
(直到 C++17*)(直到 C++17*)(直到 C++17*)(直到 C++17*)
(直到 C++20*)
(直到 C++20*)
(直到 C++17*)(直到 C++17*)
(直到 C++17*)(直到 C++17*)

(直到 C++17*)
(直到 C++17*)(直到 C++17*)(直到 C++17*)(直到 C++17*)
(直到 C++20*)
(直到 C++20*)
 
定義於標頭檔案 <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,

                  BinaryPredicate pred = BinaryPredicate() );
(C++17 起)
(C++20 起為 constexpr)

透過儲存 pat_firstpat_lastpred 的副本構造一個 std::default_searcher

引數

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

異常

BinaryPredicateForwardIt 的複製建構函式丟擲的任何異常。

std::default_searcher::operator()

template< class ForwardIt2 >

std::pair<ForwardIt2, ForwardIt2>

    operator()( ForwardIt2 first, ForwardIt2 last ) const;
(C++17 起)
(C++20 起為 constexpr)

std::search 的 Searcher 過載呼叫的成員函式,用於使用此搜尋器執行搜尋。

返回一對迭代器 i, j,其中 istd::search(first, last, pat_first, pat_last, pred)jstd::next(i, std::distance(pat_first, pat_last)),除非 std::search 返回 last (未匹配),在這種情況下 j 也等於 last

引數

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

返回值

一對迭代器,指向 [firstlast) 中第一個和最後一個位置(之後一個)的子序列,該子序列與 [pat_firstpat_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

[編輯] 參閱

搜尋一個範圍的元素首次出現的位置
(函式模板) [編輯]
Boyer-Moore 搜尋演算法實現
(類模板) [編輯]
Boyer-Moore-Horspool 搜尋演算法實現
(類模板) [編輯]