c++ STL之 for_each(权哥)
来源:互联网 发布:淘宝买二手苹果靠谱吗 编辑:程序博客网 时间:2024/05/16 07:20
STL中支持迭代器的容器可以用for_each来做循环迭代
以下是一个demo:
#include <iostream>#include <algorithm>using namespace std;#include <string>void add10(int& element){element += 10;}string print(int element){cout << element << ' ';return "芙蓉";}class add{int delta;public:add(int d):delta(d){}/*通过重载()使得类具有函数的类似函数的特性*/void operator()(int& element){element += delta;}};/*模拟for_each*/template <typename Iter, typename Func>void foreach(Iter ib, Iter ie, Func f){while(ib!=ie) f(*ib++);}int main(){int a[5] = {11,22,33,44,55};for_each(a,a+5,add10);for_each(a,a+5,print);cout<<endl;for_each(a,a+5,add(5));for_each(a,a+5,print);cout<<endl;for_each(a,a+5,add(1));for_each(a,a+5,print);cout<<endl;}/*输出:21 32 43 54 65 26 37 48 59 70 27 38 49 60 71 */
0 0
- c++ STL之 for_each(权哥)
- STL(3)之for_each算法实现
- STL 之for_each,transform
- STL 之for_each,transform
- STL算法之for_each
- STL (1)for_each 函数
- STL for_each
- STL::for_each
- stl for_each()
- STL for_each
- STL--for_each()
- STL for_each
- STL: for_each
- STL之bind2st for_each 和 transform
- STL学习之for_each的用法
- STL之for_each, cout, cout_if, find, find_if
- STL之sort和for_each的用法
- 【C++ STL学习之六】STL算法之for_each
- Data Center笔试
- API创建/更新员工联系电话
- 黑马程序员——GUI
- API创建/更新员工薪水
- 软件工程 项目总结
- c++ STL之 for_each(权哥)
- hbase thrift 定义
- vmware 配置
- Java笔试
- [Leetcode] Next Permutation (Java)
- 安装zend framework开发工具“ZF tool”
- python 抓取网页
- poj-3270-Cow Sorting-置换群
- Android中drawRect参数