且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将C ++成员函数指针传递给STL算法

更新时间:2023-02-26 16:55:12

std :: mem_fun 本身仅为成员函数提供包装器,因此将在传递给其 operator()的第一个参数的上下文中调用该包装器.话虽如此,您需要绑定 的正确实例XYZ ,然后再将函数对象传递到 std :: transform 算法:

std::mem_fun itself only povides a wrapper for a member function, so that one will be invoked in the context of the first argument passed to its operator(). Having said that, you need to bind the proper instance of XYZ prior to passing the function object to the std::transform algorithm:

XYZ xyz;

std::transform(foo.begin(), foo.end(),
               std::back_inserter(bar),
               std::bind1st(std::mem_fun(&XYZ::function), &xyz));
//                  ~~~~~~^      ~~~~~~^                  ~~~^

演示