使用STL绑定多个函数参数

RC.*_*RC. 5 c++ stl

在过去,我使用了bind1st和bind2nd函数,以便对STL容器进行直接操作.我现在有一个MyBase类指针的容器,为了简单起见,以下内容:

class X
{
public:
    std::string getName() const;
};

我想使用for_each调用以下静态函数,并将第一个和第二个参数绑定为:

StaticFuncClass :: doSomething(ptr-> getName(),funcReturningString());

我如何使用for_each并绑定此函数的两个参数?

我正在寻找以下内容:

for_each(ctr.begin(), ctr.end(), 
         bind2Args(StaticFuncClass::doSomething(), 
                   mem_fun(&X::getName), 
                   funcReturningString());

我看到Boost提供了自己的绑定功能,看起来像是在这里使用的东西,但是什么是STL解决方案?

在此先感谢您的回复.

jal*_*alf 13

当bind-syntax变得过于奇怪时,可靠的回退是定义自己的仿函数:

struct callDoSomething {
  void operator()(const X* x){
    StaticFuncClass::doSomething(x->getName(), funcReturningString());
  }
};

for_each(ctr.begin(), ctr.end(), callDoSomething());
Run Code Online (Sandbox Code Playgroud)

bind无论如何,这或多或少都是幕后功能所做的.