How to use boost::unordered_map with for_each?

Started by
12 comments, last by alvaro 11 years, 5 months ago
Hi, I'm come back.

Sorry, I found i use vs2008 in one of my computer.
So I can't use c++11 like:
<br /> for (auto entry : g_map) entry.second->doSomething(p1, p2);<br />

and
<br />for_each( g_map.begin(), g_map.end() [=](std::pair<int, std::shared_ptr<TEST>> const &foo){ foo.second->doSomething(p1, p2); } );<br />



And I want use boost::bind to solute this problem.

At first, I want to bind member variables std::pair<int,boost::shared_ptr<TEST>>::second
[source lang="cpp"]
for_each(
g_map.begin(),
g_map.end(),
boost::bind(&std::pair<int,boost::shared_ptr< TEST > >::second,_1));
[/source]
But it's failed, why?
Advertisement
I'm find the problem.

I haven't wrt the default constructor...
Yeah, you can start from that and add an outer bind that binds to &TEST::doSomething.

Is there any reason you can't upgrade your Visual Studio to a new version and start using modern language features?
Why insist on using for_each? If the syntax is so cumbersome that you (or I) can't figure it out, why not use an old-fashioned loop?

for (boost::unordered_map<int,boost::shared_ptr<TEST> >::iterator it = g_map.begin(), end = g_map.end(); it != end; ++it)
it->second->doSomething(p1, p2);

This topic is closed to new replies.

Advertisement