名稱空間
變體
操作

std::ranges::search_n

來自 cppreference.com
< cpp‎ | 演算法‎ | 範圍
 
 
演算法庫
有約束演算法與針對範圍的演算法 (C++20)
有約束的演算法,例如 ranges::copyranges::sort 等……
執行策略 (C++17)
排序及相關操作
劃分操作
排序操作
二分搜尋操作
(於已劃分範圍上)
集合操作(於已排序範圍上)
歸併操作(於已排序範圍上)
堆操作
最小/最大值操作
(C++11)
(C++17)
字典序比較操作
排列操作
C 庫
數值操作
未初始化記憶體上的操作
 
受約束演算法
此選單中的所有名稱均屬於名稱空間 std::ranges
非修改序列操作
修改序列操作
劃分操作
排序操作
二分查詢操作(在已排序的範圍內)
       
       
集合操作(於已排序範圍上)
堆操作
最小/最大值操作
       
       
排列操作
摺疊操作
數值操作
(C++23)            
對未初始化儲存的操作
返回型別
 
定義於標頭檔案 <algorithm>
呼叫簽名 (Call signature)
(1)
template< std::forward_iterator I, std::sentinel_for<I> S, class T,

          class Pred = ranges::equal_to, class Proj = std::identity >
requires std::indirectly_comparable<I, const T*, Pred, Proj>
constexpr ranges::subrange<I>
    search_n( I first, S last, std::iter_difference_t<I> count,

              const T& value, Pred pred = {}, Proj proj = {} );
(C++20 起)
(直到 C++26)
template< std::forward_iterator I, std::sentinel_for<I> S,

          class Pred = ranges::equal_to, class Proj = std::identity,
          class T = std::projected_value_t<I, Proj> >
requires std::indirectly_comparable<I, const T*, Pred, Proj>
constexpr ranges::subrange<I>
    search_n( I first, S last, std::iter_difference_t<I> count,

              const T& value, Pred pred = {}, Proj proj = {} );
(C++26 起)
(2)
template< ranges::forward_range R, class T,

          class Pred = ranges::equal_to, class Proj = std::identity >
requires std::indirectly_comparable
    <ranges::iterator_t<R>, const T*, Pred, Proj>
constexpr ranges::borrowed_subrange_t<R>
    search_n( R&& r, ranges::range_difference_t<R> count,

              const T& value, Pred pred = {}, Proj proj = {} );
(C++20 起)
(直到 C++26)
template< ranges::forward_range R,

          class Pred = ranges::equal_to, class Proj = std::identity,
          class T = std::projected_value_t<ranges::iterator_t<R>, Proj> >
requires std::indirectly_comparable
    <ranges::iterator_t<R>, const T*, Pred, Proj>
constexpr ranges::borrowed_subrange_t<R>
    search_n( R&& r, ranges::range_difference_t<R> count,

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

本頁描述的類函式實體是 演算法函式物件(非正式地稱為 niebloids),即

目錄

[編輯] 引數

first, last - 定義要檢查的元素範圍(又稱乾草堆)的迭代器-哨兵對
r - 要檢查的元素範圍(又稱乾草堆
count - 要搜尋的序列長度
value - 要搜尋的值(又稱“針”)
pred - 比較投影元素與 value 的二元謂詞
proj - 應用於要檢查的範圍元素的投影

[編輯] 返回值

1) 返回一個 std::ranges::subrange 物件,它包含範圍 [firstlast) 內的一對迭代器,這些迭代器指定找到的子序列。

如果未找到此類子序列,返回 std::ranges::subrange{last, last}

如果 count <= 0,返回 std::ranges::subrange{first, first}
2)(1) 相同,但返回型別為 ranges::borrowed_subrange_t<R>

[編輯] 複雜度

線性:最多應用謂詞和投影 ranges::distance(first, last) 次。

[編輯] 注意

如果迭代器建模 std::random_access_iterator,實現可以提高搜尋的平均效率。

特性測試 標準 特性
__cpp_lib_algorithm_default_value_type 202403 (C++26) 演算法的列表初始化

[編輯] 可能的實現

struct search_n_fn
{
    template<std::forward_iterator I, std::sentinel_for<I> S,
             class Pred = ranges::equal_to, class Proj = std::identity,
             class T = std::projected_value_t<I, Proj>>
    requires std::indirectly_comparable<I, const T*, Pred, Proj>
    constexpr ranges::subrange<I>
        operator()(I first, S last, std::iter_difference_t<I> count,
                   const T& value, Pred pred = {}, Proj proj = {}) const
    {
        if (count <= 0)
            return {first, first};
        for (; first != last; ++first)
            if (std::invoke(pred, std::invoke(proj, *first), value))
            {
                I start = first;
                std::iter_difference_t<I> n{1};
                for (;;)
                {
                    if (n++ == count)
                        return {start, std::next(first)}; // found
                    if (++first == last)
                        return {first, first}; // not found
                    if (!std::invoke(pred, std::invoke(proj, *first), value))
                        break; // not equ to value
                }
            }
        return {first, first};
    }
 
    template<ranges::forward_range R,
             class Pred = ranges::equal_to, class Proj = std::identity,
             class T = std::projected_value_t<ranges::iterator_t<R>, Proj>>
    requires std::indirectly_comparable<ranges::iterator_t<R>, const T*, Pred, Proj>
    constexpr ranges::borrowed_subrange_t<R>
        operator()(R&& r, ranges::range_difference_t<R> count,
                   const T& value, Pred pred = {}, Proj proj = {}) const
    {
        return (*this)(ranges::begin(r), ranges::end(r),
                       std::move(count), value,
                       std::move(pred), std::move(proj));
    }
};
 
inline constexpr search_n_fn search_n {};

[編輯] 示例

#include <algorithm>
#include <cassert>
#include <complex>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
 
int main()
{
    namespace ranges = std::ranges;
 
    static constexpr auto nums = {1, 2, 2, 3, 4, 1, 2, 2, 2, 1};
    constexpr int count{3};
    constexpr int value{2};
    typedef int count_t, value_t;
 
    constexpr auto result1 = ranges::search_n
    (
        nums.begin(), nums.end(), count, value
    );
    static_assert // found
    (
        result1.size() == count &&
        std::distance(nums.begin(), result1.begin()) == 6 &&
        std::distance(nums.begin(), result1.end()) == 9
    );
 
    constexpr auto result2 = ranges::search_n(nums, count, value);
    static_assert // found
    (
        result2.size() == count &&
        std::distance(nums.begin(), result2.begin()) == 6 &&
        std::distance(nums.begin(), result2.end()) == 9
    );
 
    constexpr auto result3 = ranges::search_n(nums, count, value_t{5});
    static_assert // not found
    (
        result3.size() == 0 &&
        result3.begin() == result3.end() &&
        result3.end() == nums.end()
    );
 
    constexpr auto result4 = ranges::search_n(nums, count_t{0}, value_t{1});
    static_assert // not found
    (
        result4.size() == 0 &&
        result4.begin() == result4.end() &&
        result4.end() == nums.begin()
    );
 
    constexpr char symbol{'B'};
    auto to_ascii = [](const int z) -> char { return 'A' + z - 1; };
    auto is_equ = [](const char x, const char y) { return x == y; };
 
    std::cout << "Find a sub-sequence " << std::string(count, symbol) << " in the ";
    std::ranges::transform(nums, std::ostream_iterator<char>(std::cout, ""), to_ascii);
    std::cout << '\n';
 
    auto result5 = ranges::search_n(nums, count, symbol, is_equ, to_ascii);
    if (not result5.empty())
        std::cout << "Found at position "
                  << ranges::distance(nums.begin(), result5.begin()) << '\n';
 
    std::vector<std::complex<double>> nums2{{4, 2}, {4, 2}, {1, 3}};
    #ifdef __cpp_lib_algorithm_default_value_type
        auto it = ranges::search_n(nums2, 2, {4, 2});
    #else
        auto it = ranges::search_n(nums2, 2, std::complex<double>{4, 2});
    #endif
    assert(it.size() == 2);
}

輸出

Find a sub-sequence BBB in the ABBCDABBBA
Found at position 6

[編輯] 另請參閱

尋找第一對相等的(或滿足給定謂詞的)相鄰項
(演算法函式物件)[編輯]
尋找第一個滿足特定條件的元素
(演算法函式物件)[編輯]
在特定範圍中尋找最後一次出現的元素序列
(演算法函式物件)[編輯]
搜尋一組元素中的任何一個
(演算法函式物件)[編輯]
如果一個序列是另一個序列的子序列,則返回 true
(演算法函式物件)[編輯]
尋找兩個範圍開始不同的第一個位置
(演算法函式物件)[編輯]
搜尋一個範圍的元素首次出現的位置
(演算法函式物件)[編輯]
在一個範圍內搜尋一個元素的連續 N 次副本首次出現的位置
(函式模板) [編輯]