名稱空間
變體
操作

std::adjacent_find

來自 cppreference.com
< cpp‎ | 演算法
 
 
演算法庫
有約束演算法與針對範圍的演算法 (C++20)
有約束的演算法,例如 ranges::copyranges::sort 等……
執行策略 (C++17)
排序及相關操作
劃分操作
排序操作
二分搜尋操作
(於已劃分範圍上)
集合操作(於已排序範圍上)
歸併操作(於已排序範圍上)
堆操作
最小/最大值操作
(C++11)
(C++17)
字典序比較操作
排列操作
C 庫
數值操作
未初始化記憶體上的操作
 
定義於標頭檔案 <algorithm>
template< class ForwardIt >
ForwardIt adjacent_find( ForwardIt first, ForwardIt last );
(1) (C++20 起為 constexpr)
template< class ExecutionPolicy, class ForwardIt >

ForwardIt adjacent_find( ExecutionPolicy&& policy,

                         ForwardIt first, ForwardIt last );
(2) (C++17 起)
template< class ForwardIt, class BinaryPred >

ForwardIt adjacent_find( ForwardIt first, ForwardIt last,

                         BinaryPred p );
(3) (C++20 起為 constexpr)
template< class ExecutionPolicy, class ForwardIt, class BinaryPred >

ForwardIt adjacent_find( ExecutionPolicy&& policy,
                         ForwardIt first, ForwardIt last,

                         BinaryPred p );
(4) (C++17 起)

在範圍 [firstlast) 中搜索兩個連續相等的元素。

1) 使用 operator== 比較元素。
3) 使用給定二元謂詞 p 比較元素。
2,4)(1,3),但按 policy 執行。
僅當滿足所有以下條件時,這些過載才參與過載決議

std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>true

(C++20 前)

std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>true

(C++20 起)

目錄

[編輯] 引數

first, last - 定義要檢查的元素範圍的迭代器對
策略(policy) - 要使用的 執行策略
p - 二元謂詞,如果元素應被視為相等,則返回 true。

謂詞函式的簽名應等效於以下內容:

 bool pred(const Type1 &a, const Type2 &b);

雖然簽名不需要有 const &,但函式不得修改傳遞給它的物件,並且必須能夠接受所有型別(可能是 const)的 Type1Type2 的所有值,無論 值類別 如何(因此,不允許 Type1 &,除非對於 Type1 移動等同於複製,否則也不允許 Type1(C++11 起))。
型別 Type1Type2 必須使得型別為 ForwardIt 的物件可以被解引用,然後隱式轉換為兩者。​

型別要求
-
ForwardIt 必須滿足 LegacyForwardIterator 的要求。
-
BinaryPred 必須滿足 BinaryPredicate 的要求。

[編輯] 返回值

一個迭代器,指向第一對相同元素的第一個元素,即第一個迭代器 it,使得對於 (1,2)*it == *(it + 1),或者對於 (3,4)p(*it, *(it + 1)) != false

如果沒有找到這樣的元素,則返回 last

[編輯] 複雜度

給定 result 作為 adjacent_find 的返回值,M 作為 std::distance(first, result)N 作為 std::distance(first, last)

1) 使用 operator== 進行恰好 min(M+1,N-1) 次比較。
2) 使用 operator== 進行 O(N) 次比較。
3) 應用謂詞 p 恰好 min(M+1,N-1) 次。
4) 應用謂詞 p O(N) 次。

[編輯] 異常

帶有模板引數 ExecutionPolicy 的過載按如下方式報告錯誤

  • 如果作為演算法一部分呼叫的函式丟擲異常,並且 ExecutionPolicy標準策略 之一,則呼叫 std::terminate。對於任何其他 ExecutionPolicy,行為是實現定義的。
  • 如果演算法未能分配記憶體,則丟擲 std::bad_alloc

[編輯] 可能的實現

adjacent_find (1)
template<class ForwardIt>
ForwardIt adjacent_find(ForwardIt first, ForwardIt last)
{
    if (first == last)
        return last;
 
    ForwardIt next = first;
    ++next;
 
    for (; next != last; ++next, ++first)
        if (*first == *next)
            return first;
 
    return last;
}
adjacent_find (3)
template<class ForwardIt, class BinaryPred>
ForwardIt adjacent_find(ForwardIt first, ForwardIt last, BinaryPred p)
{
    if (first == last)
        return last;
 
    ForwardIt next = first;
    ++next;
 
    for (; next != last; ++next, ++first)
        if (p(*first, *next))
            return first;
 
    return last;
}

[編輯] 示例

#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
 
int main()
{
    std::vector<int> v1{0, 1, 2, 3, 40, 40, 41, 41, 5};
 
    auto i1 = std::adjacent_find(v1.begin(), v1.end());
 
    if (i1 == v1.end())
        std::cout << "No matching adjacent elements\n";
    else
        std::cout << "The first adjacent pair of equal elements is at "
                  << std::distance(v1.begin(), i1) << ", *i1 = "
                  << *i1 << '\n';
 
    auto i2 = std::adjacent_find(v1.begin(), v1.end(), std::greater<int>());
    if (i2 == v1.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 "
                  << std::distance(v1.begin(), i2) << ", *i2 = " << *i2 << '\n';
}

輸出

The first adjacent pair of equal elements is at 4, *i1 = 40
The last element in the non-decreasing subsequence is at 7, *i2 = 41

[編輯] 缺陷報告

下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 240 C++98 謂詞被應用 std::find
(first, last, value) - first
對於 (1,3),其中 value 從未定義
應用 std::min(
(result - first) + 1,
(last - first) - 1)

[編輯] 參見

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