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?
#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));
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.
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));
}
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With