Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call member function on each element in a container

This question is a matter of style, since you can always write a for loop or something similar; however, is there a less obtrusive STL or BOOST equivalent to writing:

for (container<type>::iterator iter = cointainer.begin();
     iter != cointainer.end();
     iter++)
 iter->func();

?

Something like (imagined) this:

call_for_each(container.begin(), container.end(), &Type::func);

I think it would be 1) less typing, 2) easier to read, 3) less changes if you decided to change base type/container type.

EDIT: Thanks for your help, now, what if I wanted to pass some arguments to the member function?

like image 830
kyku Avatar asked Apr 05 '09 14:04

kyku


4 Answers

 #include <algorithm>  // for_each
 #include <functional> // bind

 // ...

 std::for_each(container.begin(), container.end(), 
                   std::bind(&Type::func));

See std::for_each and std::bind documentation for details.

Missed your edit: Anyway here is another way of achieving what you want without using Boost, if ever need be:

std::for_each(foo_vector.begin(), foo_vector.end(),
    std::bind(&Foo::func, std::placeholders::_1));
like image 141
dirkgently Avatar answered Nov 19 '22 18:11

dirkgently


You can use std::for_each or boost's foreach constructs.

Use boost's BOOST_FOREACH or BOOST_REVERSE_FOREACH when you don't want to move the logic into another function.

like image 22
Brian R. Bondy Avatar answered Nov 19 '22 18:11

Brian R. Bondy


I found out that boost bind seems to be well suited for the task, plus you can pass additional arguments to the method:

#include <iostream>
#include <functional>
#include <boost/bind.hpp>
#include <vector>
#include <algorithm>

struct Foo {
    Foo(int value) : value_(value) {
    }

    void func(int value) {
        std::cout << "member = " << value_ << " argument = " << value << std::endl;
    }

private:
    int value_;
};

int main() {
    std::vector<Foo> foo_vector;

    for (int i = 0; i < 5; i++)
        foo_vector.push_back(Foo(i));

    std::for_each(foo_vector.begin(), foo_vector.end(),
        boost::bind(&Foo::func, _1, 1));
}
like image 29
kyku Avatar answered Nov 19 '22 17:11

kyku


Since C++11 standard the BOOST approach is standardized with different syntax as the "range-based-loop":

class Foo {
    Foo(int x);
    void foo();
}
vector<int> list = {Foo(1),Foo(2),Foo(3)};
for(auto &item: list) item.foo();
like image 45
Jan Brezina Avatar answered Nov 19 '22 18:11

Jan Brezina