算法之旅,直奔<algorithm>之十八 for_each
2013-12-21 15:13
441 查看
for_each(vs2010)
这是我学习<algorithm>的第十八篇,for_each用的频率还是蛮高的。往后挑着常用的总结。
作用
for_each 的作用是对指定的一段范围的数据进行指定的操作。
原型
template<class InputIterator, class Function> Function for_each(InputIterator first, InputIterator last, Function fn) { while (first!=last) { fn (*first); ++first; } return fn; // or, since C++11: return move(fn); }
实验
实验数据{10,20,30}、
输出所有数据
代码
test.cpp
#include <iostream> // std::cout #include <algorithm> // std::for_each #include <vector> // std::vector void myfunction (int i) { // function: std::cout << ' ' << i; } int main () { std::vector<int> myvector; myvector.push_back(10); myvector.push_back(20); myvector.push_back(30); std::cout << "myvector contains:"; for_each (myvector.begin(), myvector.end(), myfunction); std::cout << '\n'; system("pause"); return 0; }
相关文章推荐
- 算法之旅,直奔<algorithm>之三 any_of
- 算法之旅,直奔<algorithm>之七 copy_if
- 算法之旅,直奔<algorithm>之十五 find
- 算法之旅,直奔<algorithm>之四 binary_search
- 算法之旅,直奔<algorithm>之十一 equal
- 算法之旅,直奔<algorithm>之十九 includes
- 算法之旅,直奔<algorithm>之二十三 none_of
- 算法之旅,直奔<algorithm>之八 copy_n
- 算法之旅,直奔<algorithm>之十 count_if
- 算法之旅,直奔<algorithm>之十三 fill
- 算法之旅,直奔<algorithm>之五 copy
- 算法之旅,直奔<algorithm>之十六 find_end
- 算法之旅,直奔<algorithm>之九 count
- 算法之旅,直奔<algorithm>之十七 find_first_of
- 算法之旅,直奔<algorithm>之二十一 max
- 算法之旅,直奔<algorithm>之十二 equal_range
- 算法之旅,直奔<algorithm>之二十二 sort
- 算法之旅,直奔<algorithm>之一 all_of
- 算法之旅,直奔<algorithm>之十 count_if
- 算法之旅,直奔<algorithm>之十五 find