类属算法for_each的功能是对序列中的每个元素施加由函数f指定的操作。
1
//
Illustrating the generic for_each algorithm
2 #include < iostream >
3 #include < cassert >
4 #include < algorithm >
5 #include < string >
6 #include < list >
7 #include < iostream >
8 using namespace std;
9
10 void print_list( string s)
11 {
12 cout << s << endl;
13 }
14
15 int main()
16 {
17 cout << " Illustrating the generic for_each algorithm. "
18 << endl;
19 list < string > dlist;
20 dlist.insert(dlist.end(), " Clark " );
21 dlist.insert(dlist.end(), " Rindt " );
22 dlist.insert(dlist.end(), " Senna " );
23
24 // Print out each list element:
25 for_each(dlist.begin(), dlist.end(), print_list);
26 return 0 ;
27 }
2 #include < iostream >
3 #include < cassert >
4 #include < algorithm >
5 #include < string >
6 #include < list >
7 #include < iostream >
8 using namespace std;
9
10 void print_list( string s)
11 {
12 cout << s << endl;
13 }
14
15 int main()
16 {
17 cout << " Illustrating the generic for_each algorithm. "
18 << endl;
19 list < string > dlist;
20 dlist.insert(dlist.end(), " Clark " );
21 dlist.insert(dlist.end(), " Rindt " );
22 dlist.insert(dlist.end(), " Senna " );
23
24 // Print out each list element:
25 for_each(dlist.begin(), dlist.end(), print_list);
26 return 0 ;
27 }