在boost for_each中运行成员函数的向量

时间:2012-10-12 13:17:00

标签: c++ boost stl lambda bind

我尝试学习提升lambda表达式,这是一个无法解决的问题。

如何在for_each中选择Holder的选定成员?

#include <iostream>
#include <string>

#include <boost/assign.hpp> 
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>

using namespace std;
using namespace boost::assign;
using namespace boost::lambda;

class Holder
{
public:
    void vRun1(std::string s){ cout << "vRun1 " << s << endl; }
    void vRun2(std::string s){ cout << "vRun2 " << s << endl; }
    void vRun3(std::string s){ cout << "vRun3 " << s << endl; }
};

// --------------------
std::map< std::string, mem_fun_ref_t<void, Holder> > replacer;


insert(replacer)
        ("buh", std::mem_fun_ref(&Holder::vRun1))
        ("mar", std::mem_fun_ref(&Holder::vRun2))
        ("heu", std::mem_fun_ref(&Holder::vRun3));


Holder hol;

如何在此处调用我在map<>注册的功能?

for_each(replacer.begin(), replacer.end(), /* bind(_1, hol, it.first) */ );

结果应为

vRun1 buh
vRun2 mar
vRun3 heu

2 个答案:

答案 0 :(得分:5)

看起来好像你稍微过度复杂了。我会这样使用boost::function<>

http://liveworkspace.org/code/b2c5a38d8c3499eefb6330a839a89d0a

上查看
#define BOOST_RESULT_OF_USE_DECLTYPE
#include <iostream>
#include <string>
#include <map>
#include <boost/function.hpp>
#include <boost/foreach.hpp>
#include <boost/phoenix.hpp>
using namespace std;

class Holder {
public:
    void vRun1(std::string s){ cout << "vRun1 " << s << endl; }
    void vRun2(std::string s){ cout << "vRun2 " << s << endl; }
    void vRun3(std::string s){ cout << "vRun3 " << s << endl; }
};

typedef std::map< std::string, boost::function<void(Holder&, std::string)> > Replacer;

int main()
{
    Replacer replacer;
    replacer["a"] = &Holder::vRun1;
    replacer["b"] = &Holder::vRun2;
    replacer["c"] = &Holder::vRun3;

    Holder hol;

    for (Replacer::const_iterator it=replacer.begin(); it != replacer.end(); ++it)
    {
        (it->second)(hol, it->first);
    }

可替换地:

    std::cout << "Using BOOST_FOREACH:\n";

    BOOST_FOREACH(Replacer::value_type& pair, replacer)
    {
        (pair.second)(hol, pair.first);
    }

甚至:

    std::cout << "Using Boost Phoenix:\n";

    namespace phx = boost::phoenix;
    using namespace phx::arg_names;

    std::for_each(replacer.begin(), replacer.end(),
            phx::bind(
                phx::bind(&Replacer::value_type::second, arg1), 
                phx::ref(hol), 
                phx::bind(&Replacer::value_type::first, arg1)));
}

输出

vRun1 a
vRun2 b
vRun3 c
Using BOOST_FOREACH:
vRun1 a
vRun2 b
vRun3 c
Using Boost Phoenix:
vRun1 a
vRun2 b
vRun3 c

答案 1 :(得分:1)

这对我有用:

#include <iostream>
#include <string>

#include <boost/assign.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
#include <boost/function.hpp>

using namespace boost::lambda;

class Holder
{
public:
    void vRun1(std::string s){ std::cout << "vRun1 " << s << std::endl; }
    void vRun2(std::string s){ std::cout << "vRun2 " << s << std::endl; }
    void vRun3(std::string s){ std::cout << "vRun3 " << s << std::endl; }
};

// --------------------
typedef std::map <std::string, 
                  boost::function<void(Holder&, std::string)> > Replacer_t;
Replacer_t replacer;
typedef Replacer_t::value_type ReplacerValue_t;

int main()
{
    boost::assign::insert(replacer)
        (std::string("buh"), &Holder::vRun1)
        (std::string("mar"), &Holder::vRun2)
        (std::string("heu"), &Holder::vRun3);


    Holder hol;

    for_each(replacer.begin(),
             replacer.end(),
             bind(protect(bind(bind(&ReplacerValue_t::second,_2),
                               _1,
                               bind(&ReplacerValue_t::first,_2))),
                  boost::ref(hol), _1));
}

这与boost :: lambda进行了大约2个小时的战斗。在C ++ 11中,我会在30秒内编写它,因此请相应地更新编译器。