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, BinaryPred p ); |
(3) | (C++20 以降 constexpr) |
| template< class ExecutionPolicy, class ForwardIt, class BinaryPred > ForwardIt adjacent_find( ExecutionPolicy&& policy, |
(4) | (C++17以降) |
範囲 [first, last) 内で、隣接する2つの等しい要素を検索します。
|
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以降) |
目次 |
[edit] パラメータ
| first, last | - | 検査する要素の範囲を定義するイテレータのペア |
| policy | - | 使用する 実行ポリシー |
| p | - | 要素が等しいと見なされる場合に true を返す二項述語。 述語関数のシグネチャは、以下と同等である必要がある。 bool pred(const Type1 &a, const Type2 &b); シグネチャは const & を持つ必要はないが、関数は渡されたオブジェクトを変更してはならず、値カテゴリに関わらず、(おそらく const の) |
| 型要件 | ||
-ForwardIt は LegacyForwardIterator の要件を満たさなければなりません。 | ||
-BinaryPred は BinaryPredicate の要件を満たす必要がある。 | ||
[edit] 戻り値
最初に見つかった同一要素のペアの最初の要素へのイテレータ。すなわち、(1,2) の場合は *it == *(it + 1)、(3,4) の場合は p(*it, *(it + 1)) != false となる最初のイテレータ it です。
そのような要素が見つからない場合は、last が返されます。
[edit] 計算量
result を adjacent_find の戻り値とし、M を std::distance(first, result)、N を std::distance(first, last) とすると
[edit] 例外
ExecutionPolicy というテンプレートパラメータを持つオーバーロードは、次のようにエラーを報告します。
- アルゴリズムの一部として呼び出された関数の実行が例外をスローし、
ExecutionPolicyが 標準ポリシー のいずれかである場合、std::terminate が呼び出されます。その他のExecutionPolicyの場合、動作は実装定義です。 - アルゴリズムがメモリの割り当てに失敗した場合、std::bad_alloc がスローされます。
[edit] 実装例
| 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; } |
[edit] 例
#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
[edit] 不具合報告
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 240 | C++98 | 述語は std::find のように適用された。 (first, last, value) - first 回 (1,3) について、value は定義されなかった。 |
std::min( (result - first) + 1, (last - first) - 1) 回 |
[edit] 関連項目
| 範囲内の連続する重複要素を削除する (関数テンプレート) | |
| (C++20) |
等しい(または指定された述語を満たす)最初の2つの隣接する項目を見つける (アルゴリズム関数オブジェクト) |