C++ 在boost中为每个函数运行成员函数向量

C++ 在boost中为每个函数运行成员函数向量,c++,boost,stl,lambda,bind,C++,Boost,Stl,Lambda,Bind,我试着学习boost lambda表达式,但这是一件不可行的事情 我如何才能为持有人的每个选定成员运行 #include <iostream> #include <string> #include <boost/assign.hpp> #include <boost/lambda/lambda.hpp> #include <boost/lambda/bind.hpp> using namespace std; using name

我试着学习boost lambda表达式,但这是一件不可行的事情

我如何才能为持有人的每个选定成员运行

#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;
结果应该是

vRun1 buh
vRun2 mar
vRun3 heu

看起来你好像把它稍微复杂了一点。我会像这样使用
boost::function

#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);
    }
这对我很有用:

#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;
阶级持有者
{
公众:

void vRun1(std::string s){std::cout映射迭代器,当反引用是
std::pair
bind(*_1)。第二,hol,(*_1)。第一)
没有帮助我会尝试for循环,但我的编译器还不支持C++11:(vasile for循环从一开始就在ANSI C中
(*_1).second
不适用于绑定。我认为正确的语法是
bind(&replacer::value\u type::second,*\u 1)
,因此组合的绑定表达式应该是
bind(bind(&replacer::value\u type::second,*\u 1),hol,bind(&replacer::value\u type::first))
。总的来说,这并不比sehe的答案更简单。@vasile为完整性增加了实际的BOOST\u FOREACH变体(并且去掉了现在无用的
mem\u fun\u ref
东西)@n.m.我也奋斗了一个小时,并且(和)想出了一个看起来不那么令人畏惧的凤凰咒语:)嗯……令人印象深刻。而且,写函子的好时机,我想说:)仍然令人印象深刻。
    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
#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));
}