运行提升for_each中的成员函数向量

Run through a vector of member functions in boost for_each

本文关键字:成员 函数 向量 each for 运行      更新时间:2023-10-16

我尝试学习提升lambda表达式,这是一件行不通的事情。

我怎样才能在for_each中以选定的持有人成员的身份运行?

#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

看起来你稍微复杂了一点。我会这样使用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
这对

我有用:

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

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