名稱空間
變體
操作

std::experimental::ranges::search_n

來自 cppreference.com
< cpp‎ | experimental‎ | ranges
 
 
實驗性
技術規範
檔案系統庫 (檔案系統 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/ranges/algorithm>
template< ForwardIterator I, Sentinel<I> S, class T,

          class Pred = ranges::equal_to<>, class Proj = ranges::identity >
    requires IndirectlyComparable<I, const T*, Pred, Proj>
I search_n( I first, S last, ranges::difference_type_t<I> count,

            const T& value, Pred pred = Pred{}, Proj proj = Proj{} );
(1) (ranges TS)
template< ForwardRange R, class T, class Pred = ranges::equal_to<>,

          class Proj = ranges::identity >
    requires IndirectlyComparable<ranges::iterator_t<R>, const T*, Pred, Proj>
ranges::safe_iterator_t<R> search_n( R&& r,
                                     ranges::difference_type_t<ranges::iterator_t<R>> count,

                                     const T& value, Pred pred = Pred{}, Proj proj = Proj{} );
(2) (ranges TS)
1) 在範圍 [firstlast) 中搜索第一個連續 count 個元素的序列,這些元素的投影值根據謂詞 pred 都等於給定值 value
2)(1),但使用 r 作為源範圍,如同使用 ranges::begin(r) 作為 firstranges::end(r) 作為 last

目錄

[編輯] 引數

first, last - 要檢查的元素範圍
r - 要檢查的元素範圍
count - 要搜尋的序列長度
value - 要搜尋的值
pred - 比較投影元素與 value 的謂詞
proj - 應用於元素的投影

[編輯] 返回值

指向在範圍 [firstlast) 中找到的序列開頭的迭代器。如果未找到此類序列,則返回與 last 比較相等的迭代器。

[編輯] 複雜度

最多 last - first 次謂詞和投影的應用。

[編輯] 可能的實現

template<ForwardIterator I, Sentinel<I> S, class T,
         class Pred = ranges::equal_to<>, class Proj = ranges::identity>
    requires IndirectlyComparable<I, const T*, Pred, Proj>
I search_n(I first, S last, ranges::difference_type_t<I> count,
           const T& value, Pred pred = Pred{}, Proj proj = Proj{})
{
    for (; first != last; ++first)
    {
        if (!ranges::invoke(pred, ranges::invoke(proj, *first), value))
            continue;
 
        I candidate = first;
        ranges::difference_type_t<I> cur_count = 0;
 
        while (true)
        {
            ++cur_count;
            if (cur_count == count)
                // success
                return candidate;
 
            ++first;
            if (first == last)
                // exhausted the list
                return first;
 
            if (!ranges::invoke(pred, ranges::invoke(proj, *first), value))
                // too few in a row
                break;
        }
    }
    return first;
}

[編輯] 示例

[編輯] 參閱

在一個範圍內搜尋一個元素的連續 N 次副本首次出現的位置
(函式模板) [編輯]
在特定範圍中尋找最後一次出現的元素序列
(函式模板) [編輯]
尋找第一個滿足特定條件的元素
(函式模板) [編輯]
搜尋元素範圍
(函式模板) [編輯]