I have:
struct Mystruct
{
void Update(float Delta);
}
typedef std::map<int, Mystruct*> TheMap;
typedef TheMap::iterator TheMapIt;
TheMap Container;
and wants to do:
for(TheMapIt It = Container.begin(), Ite = Container.end(); It != Ite; ++It)
{
It->second->Update(Delta);
}
using std::for_each, how to do this?
I think I can declare function like:
void Do(const std::pair<int, Mystruct*> Elem)
{
Elem->Update(/*problem!*/); ---> How to pass Delta in?
}
Or make another struct:
struct Doer
{
Doer(float Delta): d(Delta) {}
void operator(std::pair<int, Mystruct*> Elem)
{
Elem->Update(d);
}
}
But this requires a new struct.
What I wants to achieve is using plain std::for_each with something like std::bind_1st, std::mem_fun like the way with std::vector, is it possible?
Please consider using std way before using boost, thanks!
I’ve referenced this but it doesnt metion about member function with input…
How would I use for_each to delete every value in an STL map?
This is just a trade between coding style, for loop and for_each doesn’t make big difference, below are two other approaches besides for loop:
If you use C++11, could try lambda:
Or in C++03, you could add a member function to wrapper class then call
std::bind1standstd::mem_funWrite a functor isn’t a bad choice, why you are against it? A functor provides better design as it provides clean and clear purpose.