名稱空間
變體
操作

std::experimental::ranges::search

來自 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 I1, Sentinel<I1> S1,

          ForwardIterator I2, Sentinel<I2> S2, class Pred = ranges::equal_to<>,
          class Proj1 = ranges::identity, class Proj2 = ranges::identity >
    requires IndirectlyComparable<I1, I2, Pred, Proj1, Proj2>
I1 search( I1 first1, S1 last1, I2 first2, S2 last2,

           Pred pred = Pred{}, Proj1 proj1 = Proj1{}, Proj2 proj2 = Proj2{} );
(1) (ranges TS)
template< ForwardRange R1, ForwardRange R2, class Pred = ranges::equal_to<>,

          class Proj1 = ranges::identity, class Proj2 = ranges::identity >
    requires IndirectlyComparable<ranges::iterator_t<R1>, ranges::iterator_t<R2>,
                                  Pred, Proj1, Proj2>
ranges::safe_iterator_t<R1> search( R1&& r1, R2&& r2, Pred pred = Pred{},

                                    Proj1 proj1 = Proj1{}, Proj2 proj2 = Proj2{} );
(2) (ranges TS)
1) 在範圍 [first1last1) 中搜索序列 [first2last2) 的第一次出現。元素在分別被 proj2proj1 投影后,使用 pred 進行比較。
2)(1),但使用 r1 作為第一個源範圍,r2 作為第二個源範圍,如同使用 ranges::begin(r1) 作為 first1ranges::end(r1) 作為 last1ranges::begin(r2) 作為 first2,以及 ranges::end(r2) 作為 last2

儘管上述宣告所示,演算法宣告的實際模板引數數量和順序未指定。因此,如果在呼叫演算法時使用顯式模板引數,程式可能不可移植。

目錄

[編輯] 引數

first1, last1 - 要檢查的元素範圍
r1 - 要檢查的元素範圍
first2, last2 - 要搜尋的元素範圍
r2 - 要搜尋的元素範圍
pred - 應用於投影元素的謂詞
proj1 - 應用於第一個範圍元素的投影。
proj2 - 應用於第二個範圍元素的投影。

[編輯] 返回值

指向範圍 [first1last1) 中序列 [first2last2) 首次出現起始位置的迭代器。如果 [first2last2) 為空,則返回 first1。如果未找到此類出現,則返回一個與 last1 相等的迭代器。

[編輯] 複雜度

至多 S * N 次謂詞和各投影的應用,其中 S = last2 - first2N = last1 - first1

[編輯] 可能的實現

template<ForwardIterator I1, Sentinel<I1> S1,
         ForwardIterator I2, Sentinel<I2> S2, class Pred = ranges::equal_to<>,
         class Proj1 = ranges::identity, class Proj2 = ranges::identity>
    requires IndirectlyComparable<I1, I2, Pred, Proj1, Proj2>
I1 search(I1 first1, S1 last1, I2 first2, S2 last2,
          Pred pred = Pred{}, Proj1 proj1 = Proj1{}, Proj2 proj2 = Proj2{})
{
    for (; ; ++first1)
    {
        I1 it = first1;
        for (I2 it2 = first2; ; (void)++it, (void)++it2)
        {
            if (it2 == last2)
                return first1;
            if (it == last1)
                return it;
            if (!ranges::invoke(pred, ranges::invoke(proj1, *it),
                                      ranges::invoke(proj2, *it2)))
                break;
        }
    }
}

[編輯] 示例

[編輯] 參閱

搜尋一個範圍的元素首次出現的位置
(函式模板) [編輯]
在特定範圍中尋找最後一次出現的元素序列
(函式模板) [編輯]
如果一個集合是另一個集合的子集,則返回 true
(函式模板) [編輯]
判斷兩組元素是否相同
(函式模板) [編輯]
尋找第一個滿足特定條件的元素
(函式模板) [編輯]
如果一個範圍在字典上小於另一個範圍,則返回 true
(函式模板) [編輯]
尋找兩個範圍開始不同的第一個位置
(函式模板) [編輯]
在範圍中搜索元素的連續複製
(函式模板) [編輯]