for_each()详解

来源:互联网 发布:手机魔术充电软件 编辑:程序博客网 时间:2024/06/04 18:30

for_each使用方法详解[转]

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[] = {123};
  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 http://oomusou.cnblogs.com
 3Filename    : GenericAlgo_for_each_GlobalFunction.cpp
 4Compiler    : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
 5Description : Demo how to use for_each with global function
 6Release     : 05/11/2007 1.0
 7*/

 8#include <iostream>
 9#include <vector>
10#include <iostream>
11#include <algorithm>
12
13using namespace std;
14
15void printElem(int& elem) {
16  cout << elem << endl;
17}

18
19int main() {
20  int ia[] = {123};
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 function name而已,必須透過ptr_fun()這個function adapter將global function轉成function object,然後再用bind2nd()將參數bind成一個function object。

 1/* 
 2(C) OOMusou 2007 http://oomusou.cnblogs.com
 3Filename    : GenericAlgo_for_each_GlobalFunctionWithParameter.cpp
 4Compiler    : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
 5Description : Demo how to use for_each with global function with Parameter
 6Release     : 05/11/2007 1.0
 7*/

 8#include <iostream>
 9#include <vector>
10#include <iostream>
11#include <algorithm>
12#include <functional>
13
14using namespace std;
15
16void printElem(int elem, const char* prefix) {
17  cout << prefix << elem << endl;
18}

19
20int main() {
21  int ia[] = {123};
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 http://oomusou.cnblogs.com
 3Filename    : GenericAlgo_for_each_FunctionObject.cpp
 4Compiler    : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
 5Description : Demo how to use for_each with function object
 6Release     : 05/11/2007 1.0
 7*/

 8#include <iostream>
 9#include <vector>
10#include <iostream>
11#include <algorithm>
12
13using namespace std;
14
15struct printElem {
16  void operator() (int elem) {
17    cout << elem << endl;
18  }
 
19}
;
20
21int main() {
22  int ia[] = {123};
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的技巧接收參數。

 1/* 
 2(C) OOMusou 2007 http://oomusou.cnblogs.com
 3Filename    : GenericAlgo_for_each_FunctionObjectWithParameter.cpp
 4Compiler    : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
 5Description : Demo how to use for_each with function object with parameter
 6Release     : 05/11/2007 1.0
 7*/

 8#include <iostream>
 9#include <vector>
10#include <iostream>
11#include <algorithm>
12
13using namespace std;
14
15struct printElem {
16  const char* _prefix;
17
18  printElem(const char* prefix) : _prefix(prefix) {}
19  
20  void operator() (int elem) {
21    cout << _prefix << elem << endl;
22  }
 
23}
;
24
25int main() {
26  int ia[] = {123};
27  vector<int> ivec(ia, ia + sizeof(ia) / sizeof(int));
28  
29  for_each(ivec.begin(), ivec.end(), printElem("Element:"));
30}


執行結果 

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。

 1/* 
 2(C) OOMusou 2007 http://oomusou.cnblogs.com
 3
 4Filename    : GenericAlgo_for_each_MemberFunctionObject.cpp
 5Compiler    : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
 6Description : Demo how to use for_each with member function with object
 7Release     : 05/11/2007 1.0
 8*/

 9#include <vector>
10#include <iostream>
11#include <algorithm>
12#include <functional>
13
14using namespace std;
15
16class Door {
17public:
18  void open() const {
19    cout << "open door horizontally" << endl;
20  }

21  
22  void close() const {
23    cout << "close door horizontally" << endl;
24  }

25}
;
26
27class DoorController {
28protected:
29  vector<Door> _doorVec;
30  
31public:
32  void addDoor(Door aDoor) {
33    _doorVec.push_back(aDoor);
34  }

35  
36  void openDoor() const {
37    for_each(_doorVec.begin(), _doorVec.end(), mem_fun_ref(&Door::open));
38  }

39}
;
40
41int main() {
42  DoorController dc;
43  dc.addDoor(Door());
44  dc.addDoor(Door());
45  dc.openDoor();
46}


執行結果

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。

 1/* 
 2(C) OOMusou 2007 http://oomusou.cnblogs.com
 3
 4Filename    : GenericAlgo_for_each_MemberFunctionObjectPointer.cpp
 5Compiler    : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
 6Description : Demo how to use for_each with member function with object pointer
 7Release     : 05/11/2007 1.0
 8*/

 9#include <vector>
10#include <iostream>
11#include <algorithm>
12#include <functional>
13
14using namespace std;
15
16class AbstractDoor {
17public:
18  virtual void open() const {
19    cout << "open door horizontally" << endl;
20  }

21  
22  virtual void close() const {
23    cout << "close door horizontally" << endl;
24  }

25}
;
26
27class HorizontalDoor : public AbstractDoor {
28}
;
29
30class VerticalDoor : public AbstractDoor {
31public:
32  void open() const {
33    cout << "open door vertically" << endl;
34  }

35  
36  void close() const {
37    cout << "close door vertically" << endl;
38  }

39}
;
40
41class DoorController {
42protected:
43  vector<AbstractDoor*> _doorVec;
44  
45public:
46  void addDoor(AbstractDoor& aDoor) {
47    _doorVec.push_back(&aDoor);
48  }

49  
50  void openDoor() const {
51    for_each(_doorVec.begin(), _doorVec.end(), mem_fun(&AbstractDoor::open));
52  }

53}
;
54
55int main() {
56  DoorController dc;
57  dc.addDoor(HorizontalDoor());
58  dc.addDoor(VerticalDoor());
59  dc.openDoor();
60}


執行結果

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/* 
 2(C) OOMusou 2007 http://oomusou.cnblogs.com
 3
 4Filename    : GenericAlgo_for_each_MemberFunctionObjectPointerWithParameter.cpp
 5Compiler    : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
 6Description : Demo how to use for_each with member function with object pointer
 7Release     : 05/11/2007 1.0
 8*/

 9#include <iostream>
10#include <vector>
11#include <algorithm>
12#include <functional>
13
14using namespace std;
15
16class AbstractDoor {
17public:
18  virtual void open() const {
19    cout << "open door horizontally" << endl;
20  }

21  
22  virtual void close() const {
23    cout << "close door horizontally" << endl;
24  }

25  
26  virtual void openDoorBy(const char* name) const {
27    cout << name << " ";
28    open();
29  }

30}
;
31
32class HorizontalDoor : public AbstractDoor {
33}
;
34
35class VerticalDoor : public AbstractDoor {
36public:
37  void open() const {
38    cout << "open door vertically" << endl;
39  }

40  
41  void close() const {
42    cout << "close door vertically" << endl;
43  }

44}
;
45
46class DoorController {
47protected:
48  vector<AbstractDoor*> _doorVec;
49  
50public:
51  void addDoor(AbstractDoor& aDoor) {
52    _doorVec.push_back(&aDoor);
53  }

54  
55  void openDoor() const {
56    for_each(_doorVec.begin(), _doorVec.end(), bind2nd(mem_fun(&AbstractDoor::openDoorBy), "John"));
57  }

58}
;
59
60int main() {
61  DoorController dc;
62  dc.addDoor(HorizontalDoor());
63  dc.addDoor(VerticalDoor());
64  dc.openDoor();
65}


執行結果

1John open door horizontally
2John 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/* 
 2(C) OOMusou 2007 http://oomusou.cnblogs.com
 3Filename    : GenericAlgo_for_each_FunctionTemplate.cpp
 4Compiler    : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
 5Description : Demo how to use for_each with function template
 6Release     : 05/11/2007 1.0
 7*/

 8#include <iostream>
 9#include <vector>
10#include <iostream>
11#include <algorithm>
12
13using namespace std;
14
15template<typename T>
16void printElem(T elem) {
17  cout << elem << endl;
18}

19
20int main() {
21  int ia[] = {123};
22  vector<int> ivec(ia, ia + sizeof(ia) / sizeof(int));
23  
24  for_each(ivec.begin(), ivec.end(), printElem<int>);
25  //for_each(ivec.begin(), ivec.end(), (void(*)(int))printElem);
26}


執行結果

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/* 
 2(C) OOMusou 2007 http://oomusou.cnblogs.com
 3Filename    : GenericAlgo_for_each_FunctionTemplateWithNontypeParameter.cpp
 4Compiler    : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
 5Description : Demo how to use for_each with function template with nontype parameter
 6Release     : 05/11/2007 1.0
 7*/

 8#include <iostream>
 9#include <vector>
10#include <iostream>
11#include <algorithm>
12
13using namespace std;
14
15template<typename T, int i>
16void printElem(T elem) {
17  cout << i << ":"  << elem << endl;
18}

19
20int main() {
21  int ia[] = {123};
22  vector<int> ivec(ia, ia + sizeof(ia) / sizeof(int));
23  
24  for_each(ivec.begin(), ivec.end(), printElem<int5>);
25}


執行結果

5:1
5:2
5:3


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

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

2.Class Template
2.1 不傳入參數

 1/* 
 2(C) OOMusou 2007 http://oomusou.cnblogs.com
 3Filename    : GenericAlgo_for_each_ClassTemplate.cpp
 4Compiler    : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
 5Description : Demo how to use for_each with class template
 6Release     : 05/11/2007 1.0
 7*/

 8#include <iostream>
 9#include <vector>
10#include <iostream>
11#include <algorithm>
12#include <functional>
13
14using namespace std;
15
16template<typename T>
17class printElem : public unary_function<T, void> {
18public:
19  void operator() (T elem) {
20    cout << elem << endl;
21  }

22}
;
23
24int main() {
25  int ia[] = {123};
26  vector<int> ivec(ia, ia + sizeof(ia) / sizeof(int));
27  
28  for_each(ivec.begin(), ivec.end(), printElem<int>());
29}


執行結果

1
2
3


17行

class printElem : public unary_function<T, void> {


因為printElem只接受for_each()所傳的參數,算是單參數而已,所以public繼承了unary_function<T,void>,因為for_each的定義

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/* 
 2(C) OOMusou 2007 http://oomusou.cnblogs.com
 3Filename    : GenericAlgo_for_each_ClassTemplateWithParameter.cpp
 4Compiler    : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
 5Description : Demo how to use for_each with class template & parameter
 6Release     : 05/11/2007 1.0
 7*/

 8#include <iostream>
 9#include <vector>
10#include <iostream>
11#include <algorithm>
12#include <functional>
13
14using namespace std;
15
16template<typename T, typename U>
17class printElem : public unary_function<T, void> {
18private:
19  U _prefix;
20  
21public:
22  printElem(U prefix) : _prefix(prefix) {}
23  
24  void operator() (T elem) {
25    cout << _prefix << elem << endl;
26  }

27}
;
28
29int main() {
30  int ia[] = {123};
31  vector<int> ivec(ia, ia + sizeof(ia) / sizeof(int));
32  
33  for_each(ivec.begin(), ivec.end(), printElem<intconst char*>("Element:"));
34}


執行結果

Element:1
Element:
2
Element:
3


Conclusion
STL的for_each()事實上很好用,不過由於限制很多,所以常令很多新手卻步,本文試著將所有會遇到問題的地方都提出來討論,包括procedure based、object oriented、generics三種paradigm與for_each()的搭配都涵蓋了,希望對各位有幫助。

 

 

先看wikipedia定义:
A function object, also called a functor, functional, or functionoid,[1]  is a computer programming construct allowing an object to be invoked or called like it was an ordinary function, usually with the same syntax.
简单来将,仿函数(functor)就是一个重载了"()"运算符的struct或class,利用对象支持operator()的特性,来达到模拟函数调用效果的技术。
我们平时对一个集合类遍历的时候,例如vector,是这样做的:
for(vector<int>::const_iterator iter = ivec.begin(); iter != ivec.end(); ++iter)
{
    //do your whatever you want here
}
例如下面的代码:


#include <vector>
#include <iostream>

struct State
{
    State( int state) : m_state( state){}
    ~State(){ std::cout<< "~State(), m_state=" << m_state<< std::endl;}

    void setState(int state ){ m_state= state; }
    int getState()const{ return m_state; }

    void print()const { std::cout<< "State::print: " << m_state<< std::endl;}

private:
    int m_state;
};

int main()
{
    std::vector<State*> vect;

    vect.push_back(new State(0));
    vect.push_back(new State(1));
    vect.push_back(new State(2));
    vect.push_back(new State(3));

       std::vector<State*>::iterator it( vect.begin());
    std::vector<State*>::iterator ite( vect.end());
    for ( ; it != ite;++it )
    {
        (*it)->print();
    }
    
    
    system( "pause" );
    return 0;
}


这里的for循环语句有点冗余,想到了std::for_each ,为了使用for_each,我们需要定义一个函数,如下:

void print( State* pstate )
{
    pstate->print();
}

于是就可以简化为下面代码:
std::for_each( vect.begin(), vect.end(), &print );

上面这段代码有点丑陋,看起来不太爽,主要是函数指针的原因。
在这种应用环境下,C++有仿函数来替代,我们定义一个仿函数,如下:

struct Printer
{
    template<typename T> void operator()( T* t ) { t->print(); }
};

于是就可以简化为下面代码:
std::for_each( vect.begin(), vect.end(), Printer() );

下面,我们初步看下 for_each 的STL源码实现: 


        // TEMPLATE FUNCTION for_each

template<class _InIt,
    class _Fn1>inline
    _Fn1 for_each(_InIt _First, _InIt _Last, _Fn1 _Func)
    {    // perform function for each element

    _DEBUG_RANGE(_First, _Last);
    _DEBUG_POINTER(_Func);
    _CHECKED_BASE_TYPE(_InIt) _ChkFirst(_CHECKED_BASE(_First));
    _CHECKED_BASE_TYPE(_InIt) _ChkLast(_CHECKED_BASE(_Last));
    for (; _ChkFirst!= _ChkLast;++_ChkFirst)
        _Func(*_ChkFirst);
    return (_Func);
    }
    
上面的代码看起来挺晕菜的,这里给出 effective STL 里面的一个实现,简单明了:

template< typename InputIterator, typename Function >
Function for_each( InputIterator beg, InputIterator end, Function f) {
    while ( beg!= end )
        f( *beg++);
}


其实for_each就是一个模板函数,将for循环语句封装起来,前面两个参数都是迭代器,第三个参数是使用一个函数指针(或仿函数),
其功能是对每一个迭代器所指向的值调用仿函数。之前觉得for_each挺神秘的,其实看看源码也挺简单的。呵呵。


上面代码还是有点冗余,因为为了使用for_each还要单独定义一个函数(或仿函数),不太清爽,
呵呵,stl早为我们准备好了 mem_fun 模板函数来解决这个一个问题,于是代码再次简化为:

std::for_each( vect.begin(), vect.end(), std::mem_fun( &State::print ) );

我们一起看看 mem_fun 的STL源码实现: 

        // TEMPLATE FUNCTION mem_fun
template<class _Result,
    class _Ty> inline
    mem_fun_t<_Result, _Ty>mem_fun(_Result(_Ty::*_Pm)())
    {    // return a mem_fun_t functor adapter
    return (std::mem_fun_t<_Result, _Ty>(_Pm));
    }

mem_fun 函数实际上是调用 mem_fun_t 函数,我们接着深入看看 mem_fun_t,


        // TEMPLATE CLASS mem_fun_t
template<class _Result,
    class _Ty>
    class mem_fun_t
        : publicunary_function<_Ty*, _Result>
    {    // functor adapter (*p->*pfunc)(), non-const *pfunc
public:
    explicit mem_fun_t(_Result(_Ty::*_Pm)())
        : _Pmemfun(_Pm)
        {    // construct from pointer
        }

    _Result operator()(_Ty*_Pleft) const
        {    // call function
        return ((_Pleft->*_Pmemfun)());
        }
private:
    _Result (_Ty::*_Pmemfun)();    // the member function pointer
    };

将上面这段代码定义的写的我们好看懂一点,如下:

        // TEMPLATE CLASS mem_fun_t
template< typename _Result, typename _Ty >
class mem_fun_t: public unary_function<_Ty *, _Result>
{    
    typedef _Result (_Ty::*_Pmemfun)();
public:
    explicit mem_fun_t( _Pmemfun& pfunc)
        : m_pfun( pfunc)
    {    // construct from pointer
    }

    _Result operator()(_Ty*_Pleft) const
    {    // call function
        return ((_Pleft->*m_pfun)());
    }

private:
    _Pmemfun m_pfun; // the member function pointer

};

这样就比较清晰了,定义了仿函数mem_fun_t内部定义了一个类成员函数指针,
仿函数构造的时候将函数指针保存起来,当仿函数operator()被调用的时候,
就通过与一个类的实例关联起来从而实现了类成员函数的调用。

其调用流程是这样的,for_each把vector中的元素传送给mem_fun,
mem_fun自己产生一个仿函数mem_fun_t,然后仿函数调用其重载的()。

上述源码还有最后一个没有说明,就是unary_function,直接上源码:

        // TEMPLATE STRUCT unary_function
template<class _Arg,
    class _Result>
    struct unary_function
    {    // base class for unary functions
    typedef _Arg argument_type;
    typedef _Result result_type;
    };

就一个模板结构体。没有数据成员,非常简单。
最后,定义一个删除指针的仿函数:
struct DeletePointer
{
    template<typename T> void operator()( T* ptr ) const { delete ptr; }
};
然后调用,就一个逐一删除vector里面的所有元素了。
std::for_each( vect.begin(), vect.end(), DeletePointer() );

源码:


 

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

struct State
{
    State( int state) : m_state( state){}
    ~State(){ std::cout<< "~State(), m_state=" << m_state<< std::endl;}
    void setState(int state ){ m_state= state; }
    int getState()const{ return m_state; }
    void print()const { std::cout<< "State::print: " << m_state<< std::endl;}
private:
    int m_state;
};

void print( State* pstate)
{
    pstate->print();
}

struct Printer
{
    template<typename T>void operator()( T* t) { t->print();}
};

struct DeletePointer
{
    template<typename T>void operator()( T* ptr) const { delete ptr;}
};

int main()
{
    std::vector<State*> vect;

    vect.push_back(new State(0));
    vect.push_back(new State(1));
    vect.push_back(new State(2));
    vect.push_back(new State(3));

    std::vector<State*>::iterator it( vect.begin());
    std::vector<State*>::iterator ite( vect.end());
    for ( ; it != ite;++it )
    {
        (*it)->print();
    }

    std::for_each( vect.begin(), vect.end(),&print );
    std::for_each( vect.begin(), vect.end(), Printer());
    std::for_each( vect.begin(), vect.end(),std::mem_fun(&State::print) );
    std::for_each( vect.begin(), vect.end(), DeletePointer());
    vect.clear();

    system( "pause" );
    return 0;
}


C++ STL 学习 :更多仿函数(functor)(二)

C++标准程序库中提供了许多非常有用的预先定义好的的仿函数,了解这些将为我们的开发工作带来便利性和稳健性。

求反值:

// TEMPLATE STRUCT negate

template<class _Ty>
    struct negate
        : publicunary_function<_Ty, _Ty>
    {
// functor for unary operator-

    _Ty operator()(const _Ty& _Left)const
        {
// apply operator- to operand

        return (-_Left);
        }
    };


例如我们对一个vector中所有元素求反:

transform( vect.begin(), vect.end(), vect.begin(), std::negate<int>() ); 
   
transform参数1:第一个集合的开始(包含)
transform参数2:第一个集合的结束(不包含)
transform参数3:第二个集合的开始(包含)
transform参数4:对第一个集合中的每一个元素应用这个仿函数,仿函数的返回值放到第二个集合中
经过上面解释,那么 transform 算法就是使用 negate 仿函数,将第一个集合中的所有元素求反值之后转移到第二个集合中。这里第二个集合就是自己,那么这段程序就是对“集合中每个元素求反值”


下面看一些更为复杂的仿函数。下面一条语句操作结果是:将容器中所有小于5的元素删除。

std::remove_if( ivec.begin(), ivec.end(), std::bind2nd( std::less<int>(), 5 ) );


有点头大了,好长,好多陌生的语法。这里我们一点点解释。
std::less是一个仿函数结构体,不用多说,直接上源码,非常好懂(其实只要理解了operator()就非常好懂这些仿函数)。

// TEMPLATE STRUCT less

template<class _Ty>
    struct less
        : publicbinary_function<_Ty, _Ty,bool>
    {
// functor for operator<

    bool operator()(const _Ty& _Left, const _Ty& _Right)const
        {
// apply operator< to operands

        return (_Left< _Right);
        }
    };




再看std::bind2nd是一个函数,返回一个 std::binder2nd 的仿函数结构体,两个的源码一并放上:



        // TEMPLATE FUNCTION bind2nd

template<class _Fn2,
    class _Ty> inline
    binder2nd<_Fn2>bind2nd(const _Fn2& _Func,const _Ty& _Right)
    {
// return a binder2nd functor adapter

    typename _Fn2::second_argument_type _Val(_Right);
    return (std::binder2nd<_Fn2>(_Func, _Val));
    }


        
// TEMPLATE CLASS binder2nd

template<class _Fn2>
    class binder2nd
        : publicunary_function<typename _Fn2::first_argument_type,
            typename _Fn2::result_type>
    {
// functor adapter _Func(left, stored)

public:
    typedef unary_function<typename _Fn2::first_argument_type,
        typename _Fn2::result_type> _Base;
    typedef typename _Base::argument_type argument_type;
    typedef typename _Base::result_type result_type;

    binder2nd(const _Fn2& _Func,
        const typename _Fn2::second_argument_type& _Right)
        : op(_Func), value(_Right)
        {
// construct from functor and right operand

        }

    result_type operator()(const argument_type& _Left)const
        {
// apply functor to operands

        return (op(_Left, value));
        }

    result_type operator()(argument_type& _Left)const
        {
// apply functor to operands

        return (op(_Left, value));
        }

protected:
    _Fn2 op;
// the functor to apply

    typename _Fn2::second_argument_type value;
// the right operand

    };



bind2nd有两个参数:
第一个参数param1是一个仿函数(这里是std::less),该仿函数必须是带有两个参数的函数。
第二个参数param2一个普通参数,当binder2nd起作用的时候,param2会作为param1这个仿函数的第二个参数(bind2nd,如果是bind1st就是第一个参数)传给param1这个仿函数。

看binder2nd中的operator()重载函数:
result_type operator()(const argument_type& _Left) const
        {    // apply functor to operands
        return (op(_Left, value));
        }

这里的op就是bind2nd的第一个参数(是一个仿函数,这里是std::less),_Left就是这个仿函数被调用的时候由调用这传入(这里是remove_if),value就是bind2nd的第二个参数(这里是数值 5 )。
OK到这里remove_if中最后一个仿函数参数应该就比较清楚了。


下面说remove_if。说remove_if之前先说find_if。因为remove_if中调用了find_if。


        // TEMPLATE FUNCTION remove_if

template<class _FwdIt,
    class _Pr> inline
    _FwdIt remove_if(_FwdIt _First, _FwdIt _Last, _Pr _Pred)
    {
// remove each satisfying _Pred

    _First = std::find_if(_First, _Last, _Pred);
    if (_First == _Last)
        return (_First);
// empty sequence, all done

    else
        {
// nonempty sequence, worth doing

        _FwdIt _First1 = _First;
        return (_STDEXT unchecked_remove_copy_if(++_First1, _Last, _First, _Pred));
        }
    }
    
template<class _InIt,
    class _Pr> inline
    _InIt find_if(_InIt _First, _InIt _Last, _Pr _Pred)
    {
// find first satisfying _Pred

    _ASSIGN_FROM_BASE(_First,
        _Find_if(_CHECKED_BASE(_First), _CHECKED_BASE(_Last), _Pred));
    return (_First);
    }


        
// TEMPLATE FUNCTION find_if

template<class _InIt,
    class _Pr> inline
    _InIt _Find_if(_InIt _First, _InIt _Last, _Pr _Pred)
    {
// find first satisfying _Pred

    _DEBUG_RANGE(_First, _Last);
    _DEBUG_POINTER(_Pred);
    for (; _First!= _Last;++_First)
        if (_Pred(*_First))
            break;
    return (_First);
    }



从集合_First到_Last中找到第一个符合_Pred条件的值。这里的_Pred是一个仿函数,就是上面提到的std::bind2nd( std::less<int>(), 5 )返回的那个仿函数结构体 std::binder2nd。那么很清楚,就是找到集合中第一个小与5的元素。

找到之后,就最终会调用 _Remove_copy_if,看下面代码:


        // TEMPLATE FUNCTION remove_if

template<class _FwdIt,
    class _Pr> inline
    _FwdIt remove_if(_FwdIt _First, _FwdIt _Last, _Pr _Pred)
    {
// remove each satisfying _Pred

    _First = std::find_if(_First, _Last, _Pred);
    if (_First == _Last)
        return (_First);
// empty sequence, all done

    else
        {
// nonempty sequence, worth doing

        _FwdIt _First1 = _First;
        return (_STDEXT unchecked_remove_copy_if(++_First1, _Last, _First, _Pred));
        }
    }
    
    
    
    template<class _InIt,
    class _OutIt,
    class _Pr> inline
    _OutIt unchecked_remove_copy_if(_InIt _First, _InIt _Last, _OutIt _Dest, _Pr _Pred)
    {
// copy omitting each element satisfying _Pred

        return _STD _Remove_copy_if(_CHECKED_BASE(_First), _CHECKED_BASE(_Last), _Dest, _Pred,
            _STD _Range_checked_iterator_tag());
    }
    
    
    
    
        
// TEMPLATE FUNCTION remove_copy_if

template<class _InIt,
    class _OutIt,
    class _Pr> inline
    _OutIt _Remove_copy_if(_InIt _First, _InIt _Last, _OutIt _Dest, _Pr _Pred, _Range_checked_iterator_tag)
    {
// copy omitting each element satisfying _Pred

    _DEBUG_RANGE(_First, _Last);
    _DEBUG_POINTER(_Dest);
    _DEBUG_POINTER(_Pred);
    for (; _First!= _Last;++_First)
        if (!_Pred(*_First))
            *_Dest++= *_First;
    return (_Dest);
    }




_Remove_copy_if就很简单的一个函数了,就是每找到一个符合要求的元素就将后面所有元素前移动一位,从而实现删除这个元素的操作。

OK。至此remove_if这条语句解释清楚了。



跟remove_if类似的情况还有很多,例如:replace_if 。
看下面一条语句,就是将集合中等于





看整体程序:


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

struct Printer
{
    template< typename T >
    void operator()( T& _val)
    {
        std::cout<< _val << ", ";
    }
};

int main()
{
    std::vector<int> ivec;
    const int MAX_COUNT= 10;
    for ( int i = 1; i< MAX_COUNT; ++i )
    {
        ivec.push_back( i);
        ivec.push_back( MAX_COUNT- i );
    }

    std::cout<< "initialize : ";
    for_each( ivec.begin(), ivec.end(), Printer() );
    transform( ivec.begin(), ivec.end(), ivec.begin(),std::negate<int>());
//将所有元素求反值

    std::cout<< "\nafter negate : ";
    for_each( ivec.begin(), ivec.end(), Printer() );
    transform( ivec.begin(), ivec.end(), ivec.begin(),std::negate<int>());
//将所有元素求反值

    std::cout<< "\nafter negate twice : ";
    for_each( ivec.begin(), ivec.end(), Printer() );

    
//删除掉vector中小于5的所有元素,注意remove_if并不改变容器的大小,所以还需要调用erase来删除

    std::vector<int>::iterator iter = std::remove_if( ivec.begin(), ivec.end(),std::bind2nd(std::less<int>(), 5 ) );
    ivec.erase( iter, ivec.end());
    std::cout<< "\nafter remove_if[ < 5 ] : ";
    for_each( ivec.begin(), ivec.end(), Printer() );


    
//将集合中所有等于6的元素值改为60.

    std::replace_if( ivec.begin(), ivec.end(),std::bind2nd(std::equal_to<int>(), 6 ), 60 );
    std::cout<< "\nafter replace_if[ 6 --> 60 ] : ";
    for_each( ivec.begin(), ivec.end(), Printer() );

    std::cout<< std::endl;
    system( "pause" );
    return 0;
}
[/cpp]


输出:


 






initialize: 1, 9, 2, 8, 3, 7, 4, 6, 5, 5, 6, 4, 7, 3, 8, 2, 9, 1,
after negate : -1,-9, -2, -8,-3, -7, -4,-6, -5, -5,-6, -4, -7,-3, -8, -2,-9, -1,
after negate twice : 1, 9, 2, 8, 3, 7, 4, 6, 5, 5, 6, 4, 7, 3, 8, 2, 9, 1,
after remove_if[ < 5] : 9, 8, 7, 6, 5, 5, 6, 7, 8, 9,
after replace_if[ 6 --> 60] : 9, 8, 7, 60, 5, 5, 60, 7, 8, 9,
请按任意键继续. . .


原创粉丝点击