您的位置:首页 > 编程语言 > C语言/C++

如何正確的使用迴圈(使用for_each)? (C/C++) (STL) (template)

2010-10-26 20:15 465 查看
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;

1
2
3

當時我覺得STL什麼都好,就是以下這一串又臭又長

template<typename InputIterator, typename Function>

由以上source可知,for_each()只能配合global function和function object。

以下我們將對procedure based、object oriented、generics三種paradigm與for_each()搭配做探討。

Procedure Based與for_each()搭配
1.不傳入參數


1#include <iostream>
9#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

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 function name而已,必須透過ptr_fun()這個function adapter將global function轉成function object,然後再用bind2nd()將參數bind成一個function object。

1#include <iostream>
9#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;

Element:1
Element:2
Element:3

Object Oriented與for_each()搭配
1.不傳入參數
使用function object

1#include <iostream>
9#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

1
2
3

2.傳入參數
若使用function object,也可以將參數傳給printElem(),透過constructor的技巧接收參數。

1#include <iostream>
9#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

Element:1
Element:2
Element:3

function object有很多種寫法,但只要是function object都可以跟for_each()合作。

3.member_function與for_each()搭配
3.1 不傳入參數
本文的重點來了,在物件導向世界裡,最常用的就是for_each()配合member function,這該怎麼寫呢?直覺會這樣子寫

for_each(_doorVec.begin(), _doorVec.end(),&Door::open);

由於global function name本身就是一個pointer,所以想藉由&Door::open傳進一個address,但這樣compile並不會過,正確解法是

for_each(_doorVec.begin(), _doorVec.end(), mem_fun_ref(&Door::open));

透過mem_fun_ref()這個function adapter將member function轉成function object。
#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;

open door horizontally
open door horizontally

37行

for_each(_doorVec.begin(), _doorVec.end(), mem_fun_ref(&Door::open));

值得注意的是,mem_fun_ref()用在object的member function。若要搭配多型,vector必須放pointer,也就是得使用object pointer的member function,此時得使用mem_fun()將member function轉成function object。
#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;

open door horizontally
open door vertically

51行

for_each(_doorVec.begin(), _doorVec.end(), mem_fun(&AbstractDoor::open));

使用了mem_fun()。

3.2傳入參數
問題又來了,若要使member function也傳入參數呢?這時得使用bind2nd將function object和參數bind在一起,變成另外一個新的function object。

1#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>

using namespace std;

John open door horizontally
John open door vertically

56行

for_each(_doorVec.begin(), _doorVec.end(), bind2nd(mem_fun(&AbstractDoor::openDoorBy), "John"));

透過了bind2nd將參數結合後,成為一個新的function object。

Generics與for_each()搭配
1.Function Template
1.1不傳入參數
在泛型世界裡,那for_each()該怎麼配合function template呢?

1#include <iostream>
9#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

template<typename T>

1
2
3

若使用function template,有兩種寫法
一種是

for_each(ivec.begin(), ivec.end(), printElem<int>);

由於template function需要在compile時確定型別,所以要加上<int>確定為int型別。
另外一種寫法

for_each(ivec.begin(), ivec.end(), (void(*)(int))printElem);

template function並沒有確定型別,但轉成function pointer時,並須明確轉成int型別的function pointer。

1.2 傳入參數
若要如function object那樣能傳參數呢?funtion template是可以,不過有些限制,若使用nontype parameter,只能使用以下三種型別
1.int或enum
2.pointer:pointer to object,pointer to function,pointer to member。
3.reference:reference to object,reference to function。

1#include <iostream>
9#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

template<typename T, int i>

5:1
5:2
5:3

所以無法如function object那樣可以傳入字串或任意型別,最少在目前ISO C++標準是做不到的。

既然討論了function template,那最具威力的class template是否也能搭配for_each()?

2.Class Template
2.1 不傳入參數

1#include <iostream>
9#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;

template<typename T>

1
2
3

17行

template <class InputIterator, class UnaryFunction>
UnaryFunction for_each(InputIterator first, InputIterator last, UnaryFunction f);

傳進去的是UnaryFunction型別,第一個type parameter T表示傳入的型別,第二個type parameter void,表示回傳的型別,最後重新定義operator()。

2.2 傳入參數
若要使class template也能傳入參數,一樣利用function object的技巧,借用constructor。

1#include <iostream>
9#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;

template<typename T, typename U>

Element:1
Element:2
Element:3

Conclusion
STL的for_each()事實上很好用,不過由於限制很多,所以常令很多新手卻步,本文試著將所有會遇到問題的地方都提出來討論,包括procedure based、object oriented、generics三種paradigm與for_each()的搭配都涵蓋了,希望對各位有幫助。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: