c++ for_each 用法

主要参考:for_each的用法,转载自蕭鴻森的blog
(找不到原文的链接)

在接触C++特性Lambda时,接触到for_each,没想到这内容还挺多的,所以,先了解for_each,再学习Lambda。
这些文章其实都是在网上参考前人的博客,有些自己整理,有些不需要修改,本意是为自己学习,以备以后查阅之用。如有侵权,联系我即可。
本文代码大部分自己写过,使用MinGW。
原文非常好,建议阅读原文。这里仅摘用重要的内容

引入

先看一段不用for_each 的代码:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main()
{
    int a[] = { 1, 2, 3, 4};
    vector<int> v(a, a+sizeof(a)/sizeof(int));
    for(vector<int>::iterator itor = v.begin();itor!=v.end();++itor)
    {
        cout<<*itor<<endl;
    }
}

其中这句代码:

    for(vector<int>::iterator itor = v.begin();itor!=v.end();++itor)

略长,如果不常写,倒是很容易手生。

使用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、不传入参数

void fun(int i )
{
    cout<<i<<endl;
}

int main()
{
    int a[] = { 1, 2, 3, 4};
    vector<int> v(a, a+sizeof(a)/sizeof(int));
    for_each(v.begin(), v.end(), fun);
}

2、传入参数

要传入参数给global function ,需要使用 ptr_fun() 这个 function adapter 将global function 转成function object , 然后再用bind2nd() 将参数bind成一个function object。(这句话好拗口)。

void fun(int i, const char* str)
{
    cout<<str<<i<<endl;
}

int main()
{
    int a[] = { 1, 2, 3, 4};
    vector<int> v(a, a+sizeof(a)/sizeof(int));
    for_each(v.begin(), v.end(), bind2nd(ptr_fun(fun), "Element:"));
}

bind2nd 和 ptr_fun看这里
bind1st bind2nd的使用
【STL】ptr_fun详解

Object Oriented 与for_each 搭配

1、不传入参数,使用function object

#include<iostream>
#include<vector>
#include<algorithm>
#include<typeinfo>
using namespace std;

struct Play
{
    void operator () (int i)
    {
        cout<<i<<endl;
    }
};

int main()
{
    int a[] = { 1, 3, 4, 5};
    vector<int> vc(a, a+sizeof(a)/sizeof(int));
    for_each(vc.begin(), vc.end(), Play());
}

输出是

1
3
4
5

这里我有一点疑问, Play() 是怎么用的?
我给代码加了一点输入,查看建立和销毁对象的过程:

#include<iostream>
#include<vector>
#include<algorithm>
#include<typeinfo>
using namespace std;

struct Play
{
    Play()
    {
        cout<<"new a Play"<<endl;
    }
    Play(const Play&)
    {
        cout<<"new a copy Play"<<endl;
    }
    void operator () (int i)
    {
        cout<<i<<endl;
    }
    ~Play()
    {
        cout<<"dispose a Play"<<endl;
    }
};

int main()
{
    int a[] = { 1, 3, 4, 5};
    vector<int> vc(a, a+sizeof(a)/sizeof(int));
    for_each(vc.begin(), vc.end(), Play());
    cout<<"See something"<<endl;
}

此时输出是:

new a Play
1
3
4
5
new a copy Play
dispose a Play
dispose a Play
See something

可以看到这个过程有两个Play对象生成,但是,用于输出元素的却是第一个对象(重载() 操作符),为什么?
这时候回去看for_each的源码,就会发现,它的返回值是function,以我的猜测,应该是这样的。Play() 生成一个临时的匿名的Play对象,传入for_each 函数里,然后执行完for_each 函数后,return一个function时,Play用复制构造函数生成一个Play对象,然后两个Play对象的生命周期都结束,于是依次销毁。

2、传入参数

可以通过构造函数的技巧传入参数

#include<iostream>
#include<vector>
#include<algorithm>
#include<typeinfo>
using namespace std;

struct Play
{
    const char* str;
    Play(const char* s):str(s) {}
    void operator () (int i)
    {
        cout<<str<<i<<endl;
    }
};

int main()
{
    int a[] = { 1, 3, 4, 5};
    vector<int> vc(a, a+sizeof(a)/sizeof(int));
    for_each(vc.begin(), vc.end(), Play("Element:"));
}

Member function 与 for_each 搭配

1、不传入参数

通过mem_fun_ref() 这个funtion adapater 将 member funtion 转成 function object。

#include<iostream>
#include<vector>
#include<algorithm>
#include<typeinfo>
using namespace std;

class Door
{
public:
    void open() const
    {
        cout<<"open door horizontally"<<endl;
    }
};

class DoorController
{
protected:
    vector<Door> _doorVec;

public:
    void addDoor(Door door)
    {
        _doorVec.push_back(door);
    }

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

int main()
{
    DoorController dc;
    dc.addDoor(Door());
    dc.addDoor(Door());
    dc.openDoor();
}

输出:

open door horizontally
open door horizontally

值得注意的是,mem_fun_ref() 用在 object 的 member function。若要搭配多态,vector必须放pointer,也就是得使用object pointer的member function,此时得使用mem_fun()将member function转成function object。

2、传入参数

要使用 bind2nd

#include<iostream>
#include<vector>
#include<algorithm>
#include<typeinfo>
using namespace std;

class AbstractDoor
{
public:
    virtual void open(const char* str) const = 0; //定义纯虚函数

    virtual ~AbstractDoor() {}
};

class HorizontalDoor: public AbstractDoor
{
    public:
    void open(const char* str) const
    {
        cout<<str<<"open door horizontally"<<endl;
    }
};

class VerticalDoor: public AbstractDoor
{
    public:
    void open(const char* str) const
    {
        cout<<str<<"open door vertically"<<endl;
    }

};

class DoorController
{
protected:
    vector<AbstractDoor*> _doorVec;

public:
    void addDoor(AbstractDoor* door)
    {
        _doorVec.push_back(door);
    }

    void openDoor() const
    {
        for_each(_doorVec.begin(), _doorVec.end(), bind2nd(mem_fun(&AbstractDoor::open), "Jhon "));
    }

    ~DoorController() 
    {
        for_each(_doorVec.begin(), _doorVec.end(), [](AbstractDoor* p)
        {
            delete p;
            p = nullptr;
        });
    }
};

int main()
{
    DoorController dc;
    dc.addDoor(new HorizontalDoor());
    dc.addDoor(new VerticalDoor());
    dc.openDoor();
}

这里用到了Lambda,我会另讲
上面用到mem_fun_ref 和 mem_fun ,我不是很明白,参考:
STL中mem_fun和mem_fun_ref的用法

引用一句很重要的用法:

mem_fun_ref的作用和用法跟mem_fun一样,唯一的不同就是:当容器中存放的是对象实体的时候用mem_fun_ref,当容器中存放的是对象的指针的时候用mem_fun。

Generics与for_each()搭配

1. Funtion template

1.1 不传入参数
#include<iostream>
#include<vector>
#include<algorithm>
#include<typeinfo>
using namespace std;

template<typename T>
void play(T elem)
{
    cout<<elem<<endl;
}

int main()
{
    int a[] = {1, 3, 4, 5};
    vector<int> vc(a, a+sizeof(a)/sizeof(int));
    for_each(vc.begin(), vc.end(), play<int>);
}
1.2 传入参数
#include<iostream>
#include<vector>
#include<algorithm>
#include<typeinfo>
using namespace std;

template<typename T, char str>
void play(T elem)
{
    cout<<str<<elem<<endl;
}

int main()
{
    int a[] = {1, 3, 4, 5};
    vector<int> vc(a, a+sizeof(a)/sizeof(int));
    for_each(vc.begin(), vc.end(), play<int, 'a'>);
}

这里无法传入字串或者别的指针的类型。

2. class template

2.1不传入参数
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;

template<typename T>
class Play/*:public unary_function<T, void>*/ //我不懂为什么原文需要这个?
{
    public:
    void operator() (T elem)
    {
        cout<<elem<<endl;
    }
};
int main()
{
    int a[] = {1, 3, 4, 5};
    vector<int> vc(a, a+sizeof(a)/sizeof(int));
    for_each(vc.begin(), vc.end(), Play<int>());
}
2.2 传入参数
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;

template<typename T, typename V>
class Play/*:public unary_function<T, void>*/ //我不懂为什么原文需要这个?
{
    V _str;
    public:
    Play(V str):_str(str) {}
    void operator() (T elem)
    {
        cout<<_str<<elem<<endl;
    }
};
int main()
{
    int a[] = {1, 3, 4, 5};
    vector<int> vc(a, a+sizeof(a)/sizeof(int));
    for_each(vc.begin(), vc.end(), Play<int, const char*>("Element "));
}

END

  • 46
    点赞
  • 221
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
`std::for_each` 是 C++ 标准库中的一个算法函数,它用于对指定范围内的元素进行迭代并应用指定的操作。 `std::for_each` 的使用方法如下: ```cpp std::for_each(first, last, function); ``` 其中: - `first` 和 `last` 是表示要迭代的范围的迭代器,表示要处理的元素的起始和结束位置。这个范围包含了要应用操作的元素。 - `function` 是一个可调用对象,用于作用于范围内的每个元素。可以是函数指针、函数对象、lambda 表达式等。它可以有一个参数,该参数表示范围内的当前元素。 `std::for_each` 会依次对范围内的每个元素应用指定的操作,并且不返回任何值。它只是简单地应用函数,没有修改元素本身或返回任何结果。 以下是一个示例代码,展示了如何使用 `std::for_each`: ```cpp #include <iostream> #include <vector> #include <algorithm> void printElement(int element) { std::cout << element << " "; } int main() { std::vector<int> numbers = {1, 2, 3, 4, 5}; std::for_each(numbers.begin(), numbers.end(), printElement); return 0; } ``` 输出结果为:1 2 3 4 5 上述示例中,我们定义了一个 `printElement` 函数,用于打印每个元素。然后,我们使用 `std::for_each` 函数对 `numbers` 容器中的每个元素应用 `printElement` 函数,从而打印出所有元素。 希望这个简单的示例能够帮助你理解 `std::for_each` 函数的基本用法。如果有任何进一步的问题,请随时提出。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值