std::find, std::find_if, std::find_if_not - cppreference.com
De cppreference.com
<tbody> </tbody>
| Definido no cabeçalho <algorithm> |
||
|
|
(1) | |
|
|
(2) | |
|
|
(3) | (desde C++11) |
Estas funções encontraam o primeiro elemento no intervalo [first, last) que satisfaz critérios específicos:
Original:
These functions find the first element in the range [first, last) that satisfies specific criteria:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
1. find procura por um elemento igual a value
Original:
1. find searches for an element equal to value
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
2. find_if procura um elemento para o qual predicado p retornos true
Original:
2. find_if searches for an element for which predicate p returns true
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
3. find_if_not pesquisas de elemento para o qual predicado q retornos false
Original:
3. find_if_not searches for element for which predicate q returns false
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
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. |
| value | - | valor a comparar os elementos de Original: value to compare the elements to The text has been machine-translated via Google Translate. |
| p | - | unary predicate which returns true para o elemento desejado Original: for the required element The text has been machine-translated via Google Translate. The signature of the predicate function should be equivalent to the following:
The signature does not need to have |
| q | - | unary predicate which returns false para o elemento desejado Original: for the required element The text has been machine-translated via Google Translate. The signature of the predicate function should be equivalent to the following:
The signature does not need to have |
| Type requirements | ||
-InputIt must meet the requirements of InputIterator.
| ||
Valor de retorno
Iterador para o primeiro elemento que satisfaça a condição ou last se nenhum elemento for encontrado.
Original:
Iterator to the first element satisfying the condition or last if no such element is found.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Complexidade
No máximo last - first aplicações do predicado
Original:
At most last - first applications of the predicate
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Possível implementação
| First version |
|---|
template<class InputIt, class T> InputIt find(InputIt first, InputIt last, const T& value) { for (; first != last; ++first) { if (*first == value) { return first; } } return last; } |
| Second version |
template<class InputIt, class UnaryPredicate> InputIt find_if(InputIt first, InputIt last, UnaryPredicate p) { for (; first != last; ++first) { if (p(*first)) { return first; } } return last; } |
| Third version |
template<class InputIt, class UnaryPredicate> InputIt find_if_not(InputIt first, InputIt last, UnaryPredicate q) { for (; first != last; ++first) { if (!q(*first)) { return first; } } return last; } |
Se você não tem C + 11, equivalente a std::find_if_not é usar std::find_if com o predicado negado.
Original:
If you do not have C++11, an equivalent to std::find_if_not is to use std::find_if with the negated predicate.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
template<class InputIt, class UnaryPredicate> InputIt find_if_not(InputIt first, InputIt last, UnaryPredicate q) { return std::find_if(first, last, std::not1(q)); }
Exemplo
O exemplo a seguir localiza um número inteiro em um vetor de inteiros .
Original:
The following example finds an integer in a vector of integers.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
#include <iostream> #include <algorithm> #include <vector> int main() { int n1 = 3; int n2 = 5; std::vector<int> v{0, 1, 2, 3, 4}; auto result1 = std::find(v.begin(), v.end(), n1); auto result2 = std::find(v.begin(), v.end(), n2); if (result1 != v.end()) { std::cout << "v contains: " << n1 << '\n'; } else { std::cout << "v does not contain: " << n1 << '\n'; } if (result2 != v.end()) { std::cout << "v contains: " << n2 << '\n'; } else { std::cout << "v does not contain: " << n2 << '\n'; } }
Saída:
v contains: 3 v does not contain: 5
Veja também
encontra dois idênticos (ou algum outro relacionamento) itens adjacentes uns aos outros Original: finds two identical (or some other relationship) items adjacent to each other The text has been machine-translated via Google Translate. (modelo de função) [edit] | |
encontra a última sequência de elementos em um determinado intervalo Original: finds the last sequence of elements in a certain range The text has been machine-translated via Google Translate. (modelo de função) [edit] | |
| searches for any one of a set of elements (modelo de função) [edit] | |
encontra a primeira posição em que dois intervalos diferentes Original: finds the first position where two ranges differ The text has been machine-translated via Google Translate. (modelo de função) [edit] | |
| searches for a range of elements (modelo de função) [edit] | |