for_each

for_each
1 //对序列中的每个元素执行某操作 for_each()
2 /*template<class InputIterator, class Function>
3 Function for_each(InputIterator first, InputIterator last, Function f)
4 {
5 for ( ; first!=last; ++first ) f(*first);
6 return f;
7 }*/
8 #include<iostream>
9 #include <vector>
10  using namespace std;
11  void fun(int i){
12 cout<<i<<" ";
13 }
14
15 struct print{
16 void operator() (int x){cout<<x<<" ";}
17 }object;
18
19
20 int main()
21 {
22 int a[10]={1,2,3,4,5,6,7,8,9,0};
23 vector<int> v(a,a+10);
24 for_each(v.begin(),v.end(),fun);
25 cout<<endl;
26
27 vector<int>::iterator it;
28 for(it=v.begin();it!=v.end();it++)
29 cout<<*it<<" ";
30 cout<<endl;
31
32 for_each(v.begin(),v.end(),object);
33 cout<<endl;
34
35 system("PAUSE");
36 return 0;
37 }
原文地址:https://www.cnblogs.com/hpustudent/p/2024933.html