To jest stara wersja strony!
template <class ForwardIterator> ForwardIterator adjacent_find(ForwardIterator first, ForwardIterator last); template <class ForwardIterator, class BinaryPredicate> ForwardIterator adjacent_find(ForwardIterator first, ForwardIterator last, BinaryPredicate binary_pred);
Algorytm zawarty w Standard Template Library. Funkcja adjacent_find() domyślnie przeszukuje zakres danych określony przez iteratory start, end w celu odnalezienia dwóch idendtycznych, występujących kolejno elementów zawartych w strukturze danych. Porównanie elementów dokonywane jest przez zastosowanie operator==(). Możliwe jest także dostarczenie własnej funkcji porównującej elementy. Funkcja taka pobiera 2 argumenty i zwraca wartość logiczną TRUE ( gdy elemnty są równe) w przeciwnym przypadku false;
#include<alghoritm>
template <class ForwardIterator> ForwardIterator adjacent_find ( ForwardIterator first, ForwardIterator last ) { ForwardIterator next=first; ++next; if (first != last) while (next != last) if (*first++ == *next++) // or: if (pred(*first++,*next++)), for the pred version return first; return last; }
#include<alghoritm> int main() { vector<int> v1; for( int i = 0; i < 10; i++ ) { v1.push_back(i); // zmuszenie do zdublowania wartości 7 i 9 if( i == 7 || i == 9 ) { v1.push_back(i); } } vector<int>::iterator result; result = adjacent_find( v1.begin(), v1.end() ); if( result == v1.end() ) { cout << "Nie znaleziono dwóch identycznych kolejno występujących elemntów" << endl; } else { cout << "Znaleziona dwa identyczne kolejno występujące elementy. Pierwszy element: " << *result << endl; } result = adjacent_find (++result, v1.end()); if (result == v1.end()) { cout << "Nie znaleziono drugiej pary identycznych kolejno występujących elementów" << endl; } else { cout << "Istnieje druga para identycznych kolejno występujących elementów. Pierwszy element: " << *result << std::endl; } return 0; }
<code cpp> </cpp>