Call a member function using for_each

橙三吉。 提交于 2019-12-11 03:01:47

问题


This was my original code

#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;
}

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));
    }

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..?


回答1:


Use std::bind

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

Or use std::bind/lambdas

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

Full:

#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");
}


来源:https://stackoverflow.com/questions/18006685/call-a-member-function-using-for-each

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!