std::for_each
来自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. |
| Defined in header <algorithm>
|
||
| template< class InputIt, class UnaryFunction > UnaryFunction for_each( InputIt first, InputIt last, UnaryFunction f ); |
||
适用于给定的函数对象
f到每个迭代取值范围[first, last)的结果,排列整齐。 InputIt是一个可变的迭代器,f可以修改的元素的范围内,通过提领迭代器。如果f返回一个结果,该结果被忽略.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.
目录 |
[编辑] 参数
| first, last | - | 的范围内应用的功能
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 | - | 一元函数对象
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
| ||
[编辑] 返回值
f. (至 C++11)
std::move(f). (C++11 起)
[编辑] 复杂性
究竟
last - firstf的应用Original:
Exactly
last - first applications of fThe 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.
[编辑] 可能的实现
template<class InputIt, class UnaryFunction> UnaryFunction for_each(InputIt first, InputIt last, UnaryFunction f) { for (; first != last; ++first) { f(*first); } return f; } |
[编辑] 为例
下面的示例使用了lambda函数递增一个向量的所有元素,然后计算它们的总和:
Original:
The following example uses a 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'; }
Output:
before: 3 4 2 9 15 267 after: 4 5 3 10 16 268 sum: 306
[编辑] 另请参阅
| 某一范围的元素应用一个函数 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. (函数模板) | |
| 范围的循环 | 执行循环范围(C++11 起)
Original: executes loop over range (C++11 起) The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |