- 经验
- 218
- 分贝
- 0
- 家园分
- 311
- 在线时间:
- 16 小时
- 最后登录:
- 2013-5-8
- 帖子:
- 27
- 精华:
- 1
- 注册时间:
- 2009-4-23
- UID:
- 367587
注册:2009-4-23
|
Abstract
之前在(原創) 如何使用for_each() algorithm? (C/C++) (STL) 曾經討論過for_each(),不過當時功力尚淺,只談到了皮毛而已,這次看了effective STL的item 41、43後,對for_each()又有了更深入的了解,因此做了本篇心得報告。
Motivation
看到了eXile的C++中实现 foreach使用了巨集對foreach做改善,也看到了很多人對STL style的for_each()做討論,使我想對STL的for_each()再做了一次研究。
Introduction
學習過STL的container後,想要存取每一個iterator,你一定寫過以下的程式
#include <vector>
#include <iostream>
![]()
using namespace std;
![]()
![]() int main() {
![]() int ia[] = {1, 2, 3};
vector<int> ivec(ia, ia + sizeof(ia) / sizeof(int));
![]() for(vector<int>::const_iterator iter = ivec.begin(); iter != ivec.end(); ++iter) {
cout << *iter << endl;
}
}
執行結果
1
2
3
當時我覺得STL什麼都好,就是以下這一串又臭又長
![]() for(vector<int>::const_iterator iter = ivec.begin(); iter != ivec.end(); ++iter) {
![]()
若不常寫,一時還會寫不出來,其實若配合container,C++其實不應該這樣寫迴圈,正確的方式該使用for_each(),語法會變的相當簡單。
for_each()事實上是個function template,其實做如下[effective STL item 41]
template<typename InputIterator, typename Function>
![]() Function for_each(InputIterator beg, InputIterator end, Function f) {
while(beg != end)
f(*beg++);
}
![]()
由以上source可知,for_each()只能配合global function和function object。
以下我們將對procedure based、object oriented、generics三種paradigm與for_each()搭配做探討。
Procedure Based與for_each()搭配
1.不傳入參數
1![]() /**//*
2 (C) OOMusou 2007
3 Filename : GenericAlgo_for_each_GlobalFunction.cpp
4 Compiler : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
5 Description : Demo how to use for_each with global function
6 Release : 05/11/2007 1.0
7 */
8 #include <iostream>
9 #include <vector>
10 #include <iostream>
11 #include <algorithm>
12![]()
13 using namespace std;
14![]()
15![]() void printElem(int& elem) {
16 cout << elem << endl;
17 }
18![]()
19![]() int main() {
20![]() int ia[] = {1, 2, 3};
21 vector<int> ivec(ia, ia + sizeof(ia) / sizeof(int));
22
23 for_each(ivec.begin(), ivec.end(), printElem);
24 }
執行結果
1
2
3
23行
for_each(ivec.begin(), ivec.end(), printElem);
只需將vector::begin(),vector::end()和global function name傳給for_each()即可,再也不用for迴圈那種複雜的語法了。
2.傳入參數
若要傳參數給global function,就不能再只傳global functionname而已,必須透過ptr_fun()這個function adapter將global function轉成functionobject,然後再用bind2nd()將參數bind成一個function object。
1![]() /**//*
2 (C) OOMusou 2007
3 Filename : GenericAlgo_for_each_GlobalFunctionWithParameter.cpp
4 Compiler : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
5 Description : Demo how to use for_each with global function with Parameter
6 Release : 05/11/2007 1.0
7 */
8 #include <iostream>
9 #include <vector>
10 #include <iostream>
11 #include <algorithm>
12 #include <functional>
13![]()
14 using namespace std;
15![]()
16![]() void printElem(int elem, const char* prefix) {
17 cout << prefix << elem << endl;
18 }
19![]()
20![]() int main() {
21![]() int ia[] = {1, 2, 3};
22 vector<int> ivec(ia, ia + sizeof(ia) / sizeof(int));
23
24 for_each(ivec.begin(), ivec.end(), bind2nd(ptr_fun(printElem), "Element:"));
25 }
執行結果
Element:1
Element:2
Element:3
Object Oriented與for_each()搭配
1.不傳入參數
使用function object
1![]() /**//*
2 (C) OOMusou 2007
3 Filename : GenericAlgo_for_each_FunctionObject.cpp
4 Compiler : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
5 Description : Demo how to use for_each with function object
6 Release : 05/11/2007 1.0
7 */
8 #include <iostream>
9 #include <vector>
10 #include <iostream>
11 #include <algorithm>
12![]()
13 using namespace std;
14![]()
15![]() struct printElem {
16![]() void operator() (int elem) {
17 cout << elem << endl;
18 }
19 };
20![]()
21![]() int main() {
22![]() int ia[] = {1, 2, 3};
23 vector<int> ivec(ia, ia + sizeof(ia) / sizeof(int));
24
25 for_each(ivec.begin(), ivec.end(), printElem());
26 }
執行結果
1
2
3
2.傳入參數
若使用function object,也可以將參數傳給printElem(),透過constructor的技巧接收參數。
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);"> 1![]() /**//*
2 (C) OOMusou 2007
3 Filename : GenericAlgo_for_each_FunctionObjectWithParameter.cpp
4 Compiler : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
5 Description : Demo how to use for_each with function object with parameter
6 Release : 05/11/2007 1.0
7 */
8 #include <iostream>
9 #include <vector>
10 #include <iostream>
11 #include <algorithm>
12![]()
13 using namespace std;
14![]()
15![]() struct printElem <span id="Codehighlighter1_402_557_Open_Text">{
16 const char* _prefix;
17![]()
18![]() printElem(const char* prefix) : _prefix(prefix) {}
19
20<span style="color: rgb(0, 0, 0);">
|
|