At the moment I am running a for loop where I make a call on each element in an STL container, similar to the following.
void AddToUpdate(iterator iter, Update& Update) {...};
...
Update update;
for(iterator iter = container.begin(); iter != container.end(); ++iter)
AddToUpdate(iter, update);
I am looking at the for_each STL algorithm as it seems to fit my needs.
I was wondering if, given the use of a second input parameter to the function being applied to the container, whether it's possible to refactor this to use a standard STL algorithm without using member variables or other loopholes?
The various std::bind1st
/std::bind2nd
and the Boost.bind library were created to solve your problem (that is common to almost anyone who used the STL algorithms), but often they just seem a workaround instead of a solution.
Fortunately, with the upcoming C++ standard the long-awaited addition of lambda functions should solve definitively the problem. However, notice that, since std::for_each
calls the functor passing the dereferenced iterator (i.e. the actual value being considered) your AddToUpdate
function shouldn't accept the iterator, but the value.
In such case, it would be something like this:
Update update;
std::foreach(container.begin(); container.end(); [ & update](TypeOfTheValue Value) {
AddToUpdate(Value, update);
});