c++ - 在 boost for_each 中运行成员函数 vector

标签 c++ boost stl lambda bind

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

最佳答案

看起来你有点过于复杂了。我会用 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

关于c++ - 在 boost for_each 中运行成员函数 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12859924/

相关文章:

c++ - 使用 Bash 在 Windows 10 上使用 Visual Studio 2015 构建 libvpx

c++ - 为考试而死记硬背——我错过了什么(C++ 字符串操作)

c++ - Fedora 中 boost 的使用

c++ - boost::sregex_iterator 编译错误

c++ - 确定无序 vector<T> 是否具有所有唯一元素

c++ - 反汇编简单的Hello World程序

C++ 创建一个 vector 大小的数组,然后将 vector 复制到 C 样式数组中

C++ & boost::threads - 如何根据工作类型确定线程的优先级?

c++ - vector 中元素范围的迭代器,其属性具有特定值

c++ - std::map 一键,二值