Namespace
Varianti

std::for_each

Da cppreference.com.
< cpp‎ | algorithm

 
 
Algoritmo libreria
Funzioni
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 modifica le operazioni di sequenza
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.
all_of
any_of
none_of
(C++11)
(C++11)
(C++11)
for_each
count
count_if
mismatch
equal
Modifica delle operazioni di sequenza
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.
Partizionamento operazioni
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.
Ordinamento delle operazioni (su intervalli ordinati)
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.
Binarie (le operazioni di ricerca sui campi ordinati)
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.
Impostare le operazioni (su intervalli ordinati)
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.
Heap operazioni
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.
Minimo / massimo le operazioni
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.
Operazioni numeriche
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.
Libreria 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.
 
Elemento definito nell'header <algorithm>
template< class InputIt, class UnaryFunction >
UnaryFunction for_each( InputIt first, InputIt last, UnaryFunction f );
Applica la data f oggetto funzione al risultato di dereference ogni iteratore nel [first, last) campo, in ordine. Se InputIt è un mutevole iteratore, f può modificare gli elementi della gamma attraverso l'iteratore dereferenziato. Se f restituisce un risultato, il risultato viene ignorato.
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.

Indice

[modifica] Parametri

first, last -
la gamma per applicare la funzione
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'oggetto funzione unario da applicare
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

[modifica] Valore di ritorno

f. (fino al c++11)
std::move(f). (dal C++11)

[modifica] Complessità

Esattamente last - first applicazioni di 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.

[modifica] Possibile implementazione

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

[modifica] Esempio

L'esempio seguente utilizza un lambda funzione per incrementare tutti gli elementi di un vettore e quindi calcola una somma di essi:
Original:
The following example uses a lambda funzione 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';
}

Output:

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

[modifica] Vedi anche

applica una funzione di una serie di elementi
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.

(funzione di modello) [modifica]
gamma-ciclo for
esegue un ciclo su (dal C++11) gamma
Original:
executes loop over range (dal C++11)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[modifica]