std::adjacent_find
Aus cppreference.com
![]() |
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
definiert 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) | |
Durchsucht den Bereich
[first, last)
für zwei aufeinanderfolgende identische Elemente. Die erste Version verwendet operator==
um die Elemente zu vergleichen, verwendet die zweite Version des gegebenen binären Prädikats 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.
Inhaltsverzeichnis |
[Bearbeiten] Parameter
first, last | - | das Spektrum der Elemente zu untersuchen
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. |
Type requirements | ||
-ForwardIt must meet the requirements of ForwardIterator .
|
[Bearbeiten] Rückgabewert
ein Iterator auf der ersten der gleiche Elemente. Wenn keine Elemente gefunden werden, wird
last
zurückgegebenOriginal:
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.
[Bearbeiten] Komplexität
Genau die kleinere der
(result - first)
und ((last - 1) - first)
Anwendungen des Prädikats, wo result
ist der Rückgabewert .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.
[Bearbeiten] Mögliche Implementierung
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; } |
[Bearbeiten] Beispiel
Der folgende Code findet ein Paar äquivalenter ganze Zahlen in einem Array von 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); } }
Output:
match at: 4
[Bearbeiten] Siehe auch
removes consecutive duplicate elements in a range (Funktions-Template) |