◐ Shell
clean mode source ↗

std::find, std::find_if, std::find_if_not — cppreference.com

De cppreference.com

<tbody> </tbody>

Déclaré dans l'en-tête

<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)

Ces fonctions trouver le premier élément de la [first, last) gamme qui répond à des critères spécifiques:

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 recherches pour un élément égal à 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 recherches pour un élément pour lequel prédicat retourne p 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 recherches pour élément pour lequel prédicat retourne q 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.

Paramètres

first, last -

l'éventail des éléments à 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 éléments 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 - prédicat unéaire qui retourne ​true

pour l'élément 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 p(v) doit pouvoir être convertie à bool pour tout argument v de type (possiblement const) VT, où VT est le type de valeur de InputIt, inconditionellement de value category, et ne doit pas modifier v. Ainsi, un type-paramètre VT&n'est pas permis ainsi que pour VT sauf pour VT un déplacement est équivalent à une copie (depuis C++11). ​

q - prédicat unéaire qui retourne ​false

pour l'élément 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(v) doit pouvoir être convertie à bool pour tout argument v de type (possiblement const) VT, où VT est le type de valeur de InputIt, inconditionellement de value category, et ne doit pas modifier v. Ainsi, un type-paramètre VT&n'est pas permis ainsi que pour VT sauf pour VT un déplacement est équivalent à une copie (depuis C++11). ​

Type requirements
-InputIt must meet the requirements of InputIterator.

Retourne la valeur

Itérateur sur le premier élément satisfaisant la condition ou last si aucun élément correspondant n'est trouvé .

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.

Complexité

Tout au plus last - first applications du prédicat

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.

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

Si vous n'avez pas de C++11, un équivalent de std::find_if_not est d'utiliser std::find_if avec le prédicat nié .

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

Exemple

L'exemple suivant recherche un entier dans un vecteur d'entiers .

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

Résultat :

v contains: 3
v does not contain: 5

Voir aussi

trouve deux identiques (ou une autre relation) des éléments 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 générique) [edit]

trouve la dernière séquence d'éléments 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 générique) [edit]
searches for any one of a set of elements
(fonction générique) [edit]

trouve la première position dans laquelle deux plages différentes

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 générique) [edit]

recherches pour une série d'éléments

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 générique) [edit]