Standard C++ algorithm and functional headers

I’ve found joy in the standard C++ algorithm and functional headers. I finally found a nice, neat way to iterate through a container calling a specific function on all elements of the container! I used to do this sort of thing everywhere in my code:

for (std::list ::iterator i = roads.begin(); i != roads.end(); i++)
		i->Reverse();

This seems pretty simple, but believe me, it’s a lot of typing, especially if the contained type is complicated. Well, instead of doing that, just include algorithm and functional and then do:

std::for_each(roads.begin(), roads.end(), std::mem_fun_ref(&ROADSTRIP::Reverse));

… beautiful.

Comments are closed.

Dev Journal and Project Hosting