我有一个指针容器,我想遍历它,并调用一个带有引用参数的成员函数。如何使用STL完成这个功能?
我的当前解决方案是使用boost::bind和boost::ref来处理参数。
// Given:
// void Renderable::render(Graphics& g)
//
// There is a reference, g, in scope with the call to std::for_each
//
std::for_each(
sprites.begin(),
sprites.end(),
boost::bind(&Renderable::render, boost::ref(g), _1)
);
一个相关的问题(我从中得出了我的当前解决方案)是带有引用参数的函数的boost::bind。这个问题特别询问如何使用boost来做到这一点。我想知道如何不使用boost来做到这一点。 编辑:有一种方法可以在不使用任何
boost
的情况下完成相同的事情。通过使用std::bind
和相关功能,可以像这样在C++11兼容的编译器中编写和编译相同的代码:std::for_each(
sprites.begin(),
sprites.end(),
std::bind(&Renderable::render, std::placeholders::_1, std::ref(g))
);