std::adjacent_find
定義於標頭檔案 <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, |
(2) | (C++17 起) |
template< class ForwardIt, class BinaryPred > ForwardIt adjacent_find( ForwardIt first, ForwardIt last, |
(3) | (C++20 起為 constexpr) |
template< class ExecutionPolicy, class ForwardIt, class BinaryPred > ForwardIt adjacent_find( ExecutionPolicy&& policy, |
(4) | (C++17 起) |
在範圍 [
first,
last)
中搜索兩個連續相等的元素。
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)的 |
型別要求 | ||
-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)
[編輯] 異常
帶有模板引數 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) 次 |
[編輯] 參見
移除一個範圍中的連續重複元素 (函式模板) | |
(C++20) |
尋找第一對相等的(或滿足給定謂詞的)相鄰項 (演算法函式物件) |