[ Web Proxy ]
URL:
Viewing: https://fr.cppreference.com/cpp/algorithm/for_each [Back]  [Original]

std::for_each cppreference.com
cppreference.com
Espaces de noms
Variantes

std::for_each

De cppreference.com

<metanoindex/>

 
 
Bibliothque d'algorithmes
Fonctions
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Non-modification de la squence des oprations
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.
Modification de la squence des oprations
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.
Des oprations de partitionnement
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.
Oprations de tri (sur les gammes tris)
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.
is_sorted (C++11)
is_sorted_until (C++11)
sort
Oprations binaires de recherche (sur les gammes tris)
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.
Dfinir les oprations (sur les gammes tris)
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.
Oprations Heap
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.
Minimum / maximum de fonctionnement
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.
Oprations numriques
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.
Bibliothque C
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.
 
<tbody> </tbody>
Dclar dans l'en-tte <algorithm>
template< class InputIt, class UnaryFunction > UnaryFunction for_each( InputIt first, InputIt last, UnaryFunction f );
Applique la fonction donne f objet la suite de drfrencement chaque itration dans le [first, last) plage, dans l'ordre. Si InputIt est un itrateur modifiable, f peut modifier les lments de la gamme par l'itrateur drfrenc. Si f renvoie un rsultat, le rsultat est ignor .
Original:
Applies the given function object f to the result of dereferencing every iterator in the range [first, last), in order. If InputIt is a mutable iterator, f may modify the elements of the range through the dereferenced iterator. If f returns a result, the result is ignored.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Paramtres

first, last -
la gamme d'appliquer la fonction d'
Original:
the range to apply the function to
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
f -
l'objet de fonction unaire appliqu
Original:
the unary function object to be applied
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Type requirements
-
InputIt must meet the requirements of InputIterator.
-
UnaryFunction must meet the requirements of MoveConstructible. Does not have to be CopyConstructible

Retourne la valeur

f. (avant C++11)
std::move(f). (depuis C++11)

Complexit

Exactement last - first applications de f
Original:
Exactly last - first applications of f
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

template<class InputIt, class UnaryFunction>
UnaryFunction for_each(InputIt first, InputIt last, UnaryFunction f)
{
    for (; first != last; ++first) {
        f(*first);
    }
    return f;
}

Exemple

L'exemple suivant utilise une fonction lambda pour incrmenter tous les lments d'un vecteur, puis calcule une somme d'eux:
Original:
The following example uses a fonction lambda to increment all of the elements of a vector and then computes a sum of them:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

#include <vector>
#include <algorithm>
#include <iostream>

struct Sum {
    Sum() { sum = 0; }
    void operator()(int n) { sum += n; }

    int sum;
};

int main()
{
    std::vector<int> nums{3, 4, 2, 9, 15, 267};

    std::cout << "before: ";
    for (auto n : nums) {
        std::cout << n << " ";
    }
    std::cout << '\n';

    std::for_each(nums.begin(), nums.end(), [](int &n){ n++; });
    Sum s = std::for_each(nums.begin(), nums.end(), Sum());

    std::cout << "after:  ";
    for (auto n : nums) {
        std::cout << n << " ";
    }
    std::cout << '\n';
    std::cout << "sum: " << s.sum << '\n';
}

Rsultat :

before: 3 4 2 9 15 267
after:  4 5 3 10 16 268
sum: 306

Voir aussi

applique une fonction une srie d'lments
Original:
applies a function to 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) [edit]
de gamme pour la boucle
excute une boucle sur (depuis C++11) gamme
Original:
executes loop over range (depuis C++11)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[edit]

Web Proxy Viewer  |  New URL  |  Original Page