std::adjacent_find
提供: cppreference.com
![]() | このページは、Google 翻訳を使って英語版から機械翻訳されました。 翻訳には誤りや奇妙な言い回しがあるかもしれません。文章の上にポインタをおくと、元の文章が見れます。誤りを修正して翻訳を改善する手助けをしてください。翻訳についての説明は、ここをクリックしてください。 |
Defined in header <algorithm> | ||
template<class ForwardIt > ForwardIt adjacent_find( ForwardIt first, ForwardIt last ); | (1) | |
template<class ForwardIt, BinaryPredicate p > ForwardIt adjacent_find( ForwardIt first, ForwardIt last, BinaryPredicate p ); | (2) | |
連続する2つの同じ要素の範囲
[first, last)
を検索します。最初のバージョンは、要素を比較するoperator==
使用して、2番目のバージョンは、指定されたバイナリ述語p
を使用しています. Original:
Searches the range
[first, last)
for two consecutive identical elements. The first version uses operator==
to compare the elements, the second version uses the given binary predicate p
. The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
目次 |
[編集]パラメータ
first, last | - | 検討する要素の範囲 Original: the range of elements to examine The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
p | - | binary predicate which returns true if the elements should be treated as equal. The signature of the predicate function should be equivalent to the following: bool pred(const Type1 &a, const Type2 &b); The signature does not need to have const&, but the function must not modify the objects passed to it. |
型の要件 | ||
-ForwardIt は ForwardIterator の要求を満足しなければなりません。 |
[編集]値を返します
同一要素のうち最初のイテレータ。このような要素が見つからない場合は、
last
が返されますOriginal:
an iterator to the first of the identical elements. If no such elements are found,
last
is returnedThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[編集]複雑性
(result - first)
が戻り値である述語の((last - 1) - first)
とresult
アプリケーションのちょうど小さい.Original:
Exactly the smaller of
(result - first)
and ((last - 1) - first)
applications of the predicate where result
is the return value.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[編集]可能な実装
First version |
---|
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;} |
Second version |
template<class ForwardIt, BinaryPredicate p> ForwardIt adjacent_find(ForwardIt first, ForwardIt last, BinaryPredicate p){if(first == last){return last;} ForwardIt next = first;++next;for(next != last;++next, ++first){if(p(*first, *next)){return first;}}return last;} |
[編集]例
次のコードは、intergersの配列内の同等の整数のペアを見つけ.
Original:
The following code finds a pair of equivalent integers in an array of intergers.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
このコードを実行します
#include <algorithm>#include <iostream> int main(){std::vector<int> v1{0, 1, 2, 3, 40, 40, 5}; std::vector<int>::iterator result; result = std::adjacent_find(v1.begin(), v1.end()); if(result == v1.end()){std::cout<<"no matching adjacent elements";}else{std::cout<<"match at: "<<std::distance(v1.begin(), result);}}
出力:
match at: 4
[編集]参照
ある範囲内で連続して重複している要素を取り除きます (関数テンプレート) |