我有一个地图,我想在每个数据类型对象成员函数上执行调用。我知道如何在任何序列上执行此操作,但是,在关联容器上是否可能实现这一点?
我找到的最接近的答案是:Boost.Bind to access std::map elements in std::for_each。但是我不能在我的项目中使用boost,那么是否有STL替代品可以替换boost::bind吗?
如果不可能,我考虑创建一个临时序列指向数据对象的指针,然后在它上面调用for_each,就像这样:
我找到的最接近的答案是:Boost.Bind to access std::map elements in std::for_each。但是我不能在我的项目中使用boost,那么是否有STL替代品可以替换boost::bind吗?
如果不可能,我考虑创建一个临时序列指向数据对象的指针,然后在它上面调用for_each,就像这样:
class MyClass
{
public:
void Method() const;
}
std::map<int, MyClass> Map;
//...
std::vector<MyClass*> Vector;
std::transform(Map.begin(), Map.end(), std::back_inserter(Vector), std::mem_fun_ref(&std::map<int, MyClass>::value_type::second));
std::for_each(Vector.begin(), Vector.end(), std::mem_fun(&MyClass::Method));
看起来太混乱了,我不是很喜欢它。有什么建议吗?