std::find, std::find_if, std::find_if_not
[] |
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. |
| Dclar dans l'en-tte <algorithm>
|
||
template< class InputIt, class T > InputIt find( InputIt first, InputIt last, const T& value ); |
(1) | |
template< class InputIt, class UnaryPredicate > InputIt find_if( InputIt first, InputIt last, UnaryPredicate p ); |
(2) | |
template< class InputIt, class UnaryPredicate > InputIt find_if_not( InputIt first, InputIt last, UnaryPredicate q ); |
(3) | (depuis C++11) |
[first, last) gamme qui rpond des critres spcifiques:[first, last) that satisfies specific criteria:You can help to correct and verify the translation. Click here for instructions.
find recherches pour un lment gal valuefind searches for an element equal to valueYou can help to correct and verify the translation. Click here for instructions.
find_if recherches pour un lment pour lequel prdicat retourne p truefind_if searches for an element for which predicate p returns trueYou can help to correct and verify the translation. Click here for instructions.
find_if_not recherches pour lment pour lequel prdicat retourne q falsefind_if_not searches for element for which predicate q returns falseYou can help to correct and verify the translation. Click here for instructions.
Paramtres
| first, last | - | l'ventail des lments examiner
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. |
| value | - | valeur comparer les lments d'
Original: value to compare the elements to The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
| p | - | prdicat unaire qui retourne true pour l'lment requis . Original: for the required element The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. L'expression |
| q | - | prdicat unaire qui retourne false pour l'lment requis . Original: for the required element The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. L'expression |
| Type requirements | ||
-InputIt must meet the requirements of InputIterator.
| ||
Retourne la valeur
last si aucun lment correspondant n'est trouv .last if no such element is found.You can help to correct and verify the translation. Click here for instructions.
Complexit
last - first applications du prdicatlast - first applications of the predicateYou can help to correct and verify the translation. Click here for instructions.
Mise en uvre possible
| 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;
}
|
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));
}
|
Exemple
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';
}
}
Rsultat :
v contains: 3
v does not contain: 5
Voir aussi
trouve deux identiques (ou une autre relation) des lments adjacents les uns aux autres Original: finds two identical (or some other relationship) items adjacent to each other The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (fonction gnrique) | |
trouve la dernire squence d'lments dans une certaine plage Original: finds the last sequence of elements in a certain range The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (fonction gnrique) | |
| searches for any one of a set of elements (fonction gnrique) | |
trouve la premire position dans laquelle deux plages diffrentes Original: finds the first position where two ranges differ The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (fonction gnrique) | |
recherches pour une srie d'lments Original: searches for a range of elements The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (fonction gnrique) | |