Use of for_each on map elements

Posted by Antonio on Stack Overflow See other posts from Stack Overflow or by Antonio
Published on 2010-05-17T15:18:54Z Indexed on 2010/05/17 15:30 UTC
Read the original article Hit count: 469

Filed under:
|
|
|
|

I have a map where I'd like to perform a call on every data type object member function. I yet know how to do this on any sequence but, is it possible to do it on an associative container?

The closest answer I could find was this: Boost.Bind to access std::map elements in std::for_each. But I cannot use boost in my project so, is there an STL alternative that I'm missing to boost::bind?

If not possible, I thought on creating a temporary sequence for pointers to the data objects and then, call for_each on it, something like this:

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));

It looks too obfuscated and I don't really like it. Any suggestions?

© Stack Overflow or respective owner

Related posts about c++

Related posts about stl