我尝试学习 boost 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