std:: adjacent_find
|
ヘッダー
<algorithm>
で定義
|
||
|
template
<
class
ForwardIt
>
ForwardIt adjacent_find ( ForwardIt first, ForwardIt last ) ; |
(1) | (constexpr since C++20) |
|
template
<
class
ExecutionPolicy,
class
ForwardIt
>
ForwardIt adjacent_find
(
ExecutionPolicy
&&
policy,
|
(2) | (since C++17) |
|
template
<
class
ForwardIt,
class
BinaryPred
>
ForwardIt adjacent_find
(
ForwardIt first, ForwardIt last,
|
(3) | (constexpr since C++20) |
|
template
<
class
ExecutionPolicy,
class
ForwardIt,
class
BinaryPred
>
ForwardIt adjacent_find
(
ExecutionPolicy
&&
policy,
|
(4) | (since 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以降) |
目次 |
パラメータ
| first, last | - | 検査する要素の範囲を定義するイテレータのペア |
| policy | - | 使用する実行ポリシー |
| p | - |
要素が等しいと扱われるべき場合に
true
を返す二項述語
述語関数のシグネチャは以下と同等であるべき: bool pred ( const Type1 & a, const Type2 & b ) ;
シグネチャが
const
&
を持つ必要はないが、関数は渡されたオブジェクトを変更してはならず、
値カテゴリ
に関係なく型
|
| 型要件 | ||
-
ForwardIt
は
LegacyForwardIterator
の要件を満たさなければならない。
|
||
-
BinaryPred
は
BinaryPredicate
の要件を満たさなければならない。
|
||
戻り値
最初の同一要素ペアの最初の要素へのイテレータ、すなわち it が * it == * ( it + 1 ) を満たす最初のイテレータ( (1,2) の場合)、または p ( * it, * ( it + 1 ) ) ! = false を満たす最初のイテレータ( (3,4) の場合)。
そのような要素が見つからない場合、 last が返されます。
計算量
adjacent_find
の戻り値として
result
が与えられ、
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++規格に対して遡及的に適用されました。
| DR | 適用対象 | 公開時の動作 | 正しい動作 |
|---|---|---|---|
| LWG 240 | C++98 |
述語が
std::
find
( first, last, value ) - first 回適用された バージョン (1,3) において、 value が定義されていなかった |
std::
min
(
( result - first ) + 1 , ( last - first ) - 1 ) 回適用される |
関連項目
|
連続する重複要素を範囲から削除する
(関数テンプレート) |
|
|
(C++20)
|
等しい(または指定された述語を満たす)最初の2つの隣接する要素を検索する
(アルゴリズム関数オブジェクト) |