dlanod
dlanod

Reputation: 8980

Is it possible to use the STL for_each algorithm using a function with independent input parameters?

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?

Upvotes: 1

Views: 334

Answers (2)

Pawel Zubrycki
Pawel Zubrycki

Reputation: 2713

You want to use std::bind2nd() - http://www.cplusplus.com/reference/std/functional/bind2nd/

Basically it return a unary function object from function with 2 arguments, where the second argument is fixed.

This is how your code should look like with for_each and bind2nd:

Update update;
for_each(container.begin(), container.end(), bind2nd(ptr_fun(AddToUpdate), update));

Edit. As Matteo noticed the first argument of AddToUpdate has to be type of value in container, not an iterator.

Upvotes: 4

Matteo Italia
Matteo Italia

Reputation: 126777

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

Upvotes: 4

Related Questions