使用for_each调用成员函数 [英] Call a member function using for_each

查看:90
本文介绍了使用for_each调用成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的原始代码

#include "stdafx.h"
#include <string>
#include <list>
#include <algorithm>
using namespace std;


class testing
{
public:
    int value;
    testing(int v)
    {
        value = v;
    }

    int getval()
    {
        return(value);
    }

};

void func(testing& ob)
{
    printf("The value is %d\n", ob.value);
}

int _tmain(int argc, _TCHAR* argv[])
{
    std::list<testing> testvar[3];

    testing t1(0);
    testing t2(1);
    testing t3(3);

    testvar[0].push_back(t1);
    testvar[0].push_back(t2);
    testvar[0].push_back(t3);

    std::for_each(testvar[0].begin(), testvar[0].end(), func);

    printf("Reached End");
    getchar();
    return 0;
}

我对其进行了修改,使func成为成员函数,并且出现了奇怪的编译错误,我在网上进行搜索,有人告诉使用bind1st,bind2nd

I modified it to make func a member function and got weird compile errors, I searched online and someone had told use bind1st, bind2nd

#include "stdafx.h"
#include <string>
#include <list>
#include <algorithm>
using namespace std;

class testing
{
public:
int value;
testing(int v)
{
    value = v;
}

int getval()
{
    return(value);
}

};

class testing2
{
public:
    std::list<testing> testvar[3];

    void func(testing& ob)
    {
        printf("The value is %d\n", ob.value);
    }

    void driver()
    {
        std::for_each(testvar[0].begin(), testvar[0].end(), func);
    }

};



int _tmain(int argc, _TCHAR* argv[])
{
    testing2 testob;

    testob.driver();


    printf("Reached End");
    getchar();
    return 0;
}

所以我修改了驱动程序功能

So I modified the driver function to this

    void driver()
    {
std::for_each(testvar[0].begin(), testvar[0].end(),     std::bind1st(std::mem_fun(&testing2::func), this));
    }

我仍然得到一些奇怪的编译错误,有人可以解释一下为什么我们需要调用成员函数是这种奇怪的方式吗?以及bind1st如何帮助..?

I still get some weird compile erros, could someone please expain why we need to call a member function is such weird way..? and how does bind1st help..?

推荐答案

使用std :: bind

Use std::bind

 std::for_each(testvar[0].begin(), testvar[0].end(), std::bind(&testing2::func, this, std::placeholders::_1));

或使用std :: bind/lambdas

Or use std::bind/lambdas

 std::for_each(testvar[0].begin(), testvar[0].end(), [this](testing& ob) { func(ob); });

完整:

#include <string>
#include <list>
#include <algorithm>
using namespace std;

struct testing {
    int value;
    testing(int v) { value = v; }
    int getval() { return(value); }
};

struct testing2 {
    std::list<testing> testvar[3];

    void func(testing& ob) {
        printf("The value is %d\n", ob.value);
    }

    void driver() {
        auto f = std::bind(&testing2::func, this, std::placeholders::_1);
        std::for_each(testvar[0].begin(), testvar[0].end(), f);
        std::for_each(testvar[0].begin(), testvar[0].end(), [this](testing& ob) { func(ob); });
    }
};

int main() {
    testing2 testob;
    testob.driver();
    printf("Reached End");
}

这篇关于使用for_each调用成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆