Espaços nominais
Variantes
Acções

std::adjacent_find

Da cppreference.com
< cpp‎ | algorithm

 
 
Biblioteca algoritmo
Funções
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Não modificar operações de seqüência
Original:
Non-modifying sequence operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Modificando operações de seqüência
Original:
Modifying sequence operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Particionamento operações
Original:
Partitioning operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Operações de classificação (em intervalos ordenados)
Original:
Sorting operations (on sorted ranges)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Binários operações de busca (em intervalos ordenados)
Original:
Binary search operations (on sorted ranges)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Definir operações (em intervalos ordenados)
Original:
Set operations (on sorted ranges)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Operações de pilha
Original:
Heap operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Mínimo / máximo de operações
Original:
Minimum/maximum operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Operações numéricas
Original:
Numeric operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
C biblioteca
Original:
C library
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
 
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)
Pesquisa o [first, last) faixa por duas consecutivas elementos idênticos. A primeira versão utiliza operator== para comparar os elementos, a segunda versão usa o predicado binário dado 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.

Índice

[editar] Parâmetros

first, last -
a gama de elementos para examinar
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.
The types  Type1 and  Type2 must be such that an object of type ForwardIt can be dereferenced and then implicitly converted to both of them.

Type requirements
-
ForwardIt must meet the requirements of ForwardIterator.

[editar] Valor de retorno

um iterador para o primeiro dos elementos idênticos. Se não houver tais elementos são encontrados, last é devolvido
Original:
an iterator to the first of the identical elements. If no such elements are found, last is returned
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[editar] Complexidade

Exatamente as aplicações menores de (result - first) e ((last - 1) - first) do predicado onde result é o valor de retorno.
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.

[editar] Possível implementação

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;
}

[editar] Exemplo

O código a seguir encontra um par de inteiros equivalentes em uma matriz de 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.

#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

[editar] Veja também

removes consecutive duplicate elements in a range
(modelo de função) [edit]