Let’s say I have the following object:
struct Foo
{
int size() { return 2; }
};
What’s the best way (most maintainable, readable, etc.) to get the total size of all objects in a vector<Foo>? I’ll post my solution but I’m interested in better ideas.
Update:
So far we have:
- std::accumulate and a functor
- std::accumulate and a lambda expression
- plain ol’ for-loop
Are there any other workable solutions? Can you make something maintainable using boost::bind or std::bind1st/2nd?
In addition to your own suggestion, if your compiler supports C++0x lambda expressions, you can use this shorter version: