std::for_each
Da cppreference.com
![]() |
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. |
Definido no cabeçalho <algorithm>
|
||
template< class InputIt, class UnaryFunction > UnaryFunction for_each( InputIt first, InputIt last, UnaryFunction f ); |
||
Aplica-se a
f
determinado objeto função do resultado de cada dereferencing iterador na [first, last)
gama, em ordem. Se InputIt
é mutável iterador, f
pode modificar os elementos da gama através do iterador dereferenced. Se f
retorna um resultado, o resultado é ignorado.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.
You can help to correct and verify the translation. Click here for instructions.
Índice |
[editar] Parâmetros
first, last | - | a gama de aplicar a função de
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 | - | o objeto de função unária a ser aplicada
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
|
[editar] Valor de retorno
f
. (até C++11)
std::move(f). (desde C++11)
[editar] Complexidade
Exactamente
last
- first
aplicações 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.
You can help to correct and verify the translation. Click here for instructions.
[editar] Possível implementação
template<class InputIt, class UnaryFunction> UnaryFunction for_each(InputIt first, InputIt last, UnaryFunction f) { for (; first != last; ++first) { f(*first); } return f; } |
[editar] Exemplo
O exemplo a seguir utiliza um função lambda para aumentar todos os elementos de um vector e, em seguida, calcula uma soma deles:
Original:
The following example uses a função 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.
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'; }
Saída:
before: 3 4 2 9 15 267 after: 4 5 3 10 16 268 sum: 306
[editar] Veja também
aplica-se uma função de uma série de elementos 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. (modelo de função) | |
faixa para loop | executa loop sobre (desde C++11) alcance
Original: executes loop over range (desde C++11) The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |