首页 > 其他 > 详细

std::for_each

时间:2014-09-12 23:28:34      阅读:382      评论:0      收藏:0      [点我收藏+]
 1 #include <algorithm>
 2 
 3 #include <iostream>
 4 #include <vector>
 5 #include <string>
 6 
 7 using std::vector;
 8 using std::string;
 9 using std::cout;
10 using std::endl;
11 
12 template<class InputIter, class Func>
13 Func LTM_for_each(InputIter first, InputIter last, Func func)
14 {
15     while (first != last)
16     {
17         func(*first);
18         ++first;
19     }
20     return func;
21 }
22 
23 void helperFunction(string& str)
24 {
25     str += ".cpp";
26 }
27 
28 void print(vector<string> vec)
29 {
30     vector<string>::iterator iter;
31     for (iter = vec.begin(); iter != vec.end(); iter++)
32     {
33         cout << *iter << endl;
34     }
35     cout << \n;
36 }
37 
38 int main(void)
39 {
40     vector<string> vec;
41     vec.push_back("a");
42     vec.push_back("b");
43     vec.push_back("c");
44     vec.push_back("d");
45     print(vec);
46 
47     // for_each(vec.begin(), vec.end(), helperFunction);
48     LTM_for_each(vec.begin(), vec.end(), helperFunction);
49     print(vec);
50 
51     return 0;
52 }

 

std::for_each

原文:http://www.cnblogs.com/chinshing/p/3969298.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!