名稱空間
變體
操作

std::ranges::adjacent_find

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

          std::indirect_binary_predicate<
              std::projected<I, Proj>,
              std::projected<I, Proj>> Pred = ranges::equal_to >
constexpr I

    adjacent_find( I first, S last, Pred pred = {}, Proj proj = {} );
(1) (C++20 起)
template< ranges::forward_range R, class Proj = std::identity,

          std::indirect_binary_predicate<
              std::projected<ranges::iterator_t<R>, Proj>,
              std::projected<ranges::iterator_t<R>, Proj>> Pred = ranges::equal_to >
constexpr ranges::borrowed_iterator_t<R>

    adjacent_find( R&& r, Pred pred = {}, Proj proj = {} );
(2) (C++20 起)

在範圍 [firstlast) 中搜索第一對連續相等的元素。

1) 元素使用 pred 進行比較(在透過投影 proj 投影之後)。
2)(1) 相同,但使用 r 作為源範圍,如同使用 ranges::begin(r) 作為 first 且使用 ranges::end(r) 作為 last

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

目錄

[編輯] 引數

first, last - 定義要檢查的元素 範圍 的迭代器-哨兵對
r - 要檢查的元素範圍
pred - 應用於投影元素的謂詞
proj - 應用於元素的投影

[編輯] 返回值

指向第一對相同元素的第一個元素的迭代器,即第一個迭代器 it,使得 bool(std::invoke(pred, std::invoke(proj1, *it), std::invoke(proj, *(it + 1))))true

如果未找到此類元素,則返回等於 last 的迭代器。

[編輯] 複雜度

謂詞和投影的精確應用次數為 min((result - first) + 1, (last - first) - 1),其中 result 是返回值。

[編輯] 可能的實現

struct adjacent_find_fn
{
    template<std::forward_iterator I, std::sentinel_for<I> S, class Proj = std::identity,
             std::indirect_binary_predicate<
                 std::projected<I, Proj>,
                 std::projected<I, Proj>> Pred = ranges::equal_to>
    constexpr I operator()(I first, S last, Pred pred = {}, Proj proj = {}) const
    {
        if (first == last)
            return first;
        auto next = ranges::next(first);
        for (; next != last; ++next, ++first)
            if (std::invoke(pred, std::invoke(proj, *first), std::invoke(proj, *next)))
                return first;
        return next;
    }
 
    template<ranges::forward_range R, class Proj = std::identity,
             std::indirect_binary_predicate<
                 std::projected<ranges::iterator_t<R>, Proj>,
                 std::projected<ranges::iterator_t<R>, Proj>> Pred = ranges::equal_to>
    constexpr ranges::borrowed_iterator_t<R>
        operator()(R&& r, Pred pred = {}, Proj proj = {}) const
    {
        return (*this)(ranges::begin(r), ranges::end(r), std::ref(pred), std::ref(proj));
    }
};
 
inline constexpr adjacent_find_fn adjacent_find;

[編輯] 示例

#include <algorithm>
#include <functional>
#include <iostream>
#include <ranges>
 
constexpr bool some_of(auto&& r, auto&& pred) // some but not all
{
    return std::ranges::cend(r) != std::ranges::adjacent_find(r,
        [&pred](auto const& x, auto const& y)
        {
            return pred(x) != pred(y);
        });
}
 
// test some_of
constexpr auto a = {0, 0, 0, 0}, b = {1, 1, 1, 0}, c = {1, 1, 1, 1};
auto is_one = [](auto x){ return x == 1; };
static_assert(!some_of(a, is_one) && some_of(b, is_one) && !some_of(c, is_one));
 
int main()
{
    const auto v = {0, 1, 2, 3, 40, 40, 41, 41, 5}; /*
                                ^^          ^^       */
    namespace ranges = std::ranges;
 
    if (auto it = ranges::adjacent_find(v.begin(), v.end()); it == v.end())
        std::cout << "No matching adjacent elements\n";
    else
        std::cout << "The first adjacent pair of equal elements is at ["
                  << ranges::distance(v.begin(), it) << "] == " << *it << '\n';
 
    if (auto it = ranges::adjacent_find(v, ranges::greater()); it == v.end())
        std::cout << "The entire vector is sorted in ascending order\n";
    else
        std::cout << "The last element in the non-decreasing subsequence is at ["
                  << ranges::distance(v.begin(), it) << "] == " << *it << '\n';
}

輸出

The first adjacent pair of equal elements is at [4] == 40
The last element in the non-decreasing subsequence is at [7] == 41

[編輯] 參閱

移除一個範圍中的連續重複元素
(演算法函式物件)[編輯]
尋找第一對相等的(或滿足給定謂詞的)相鄰項
(函式模板) [編輯]