【发布时间】:2011-03-13 09:40:50
【问题描述】:
假设我有以下对象:
struct Foo
{
int size() { return 2; }
};
获得vector<Foo> 中所有对象的总size 的最佳方法是什么(最可维护、可读等)?我会发布我的解决方案,但我对更好的想法感兴趣。
更新:
到目前为止,我们有:
- std::accumulate 和仿函数
- std::accumulate 和 lambda 表达式
- 普通的 for 循环
还有其他可行的解决方案吗?你能用boost::bind 或std::bind1st/2nd 做一些可维护的东西吗?
【问题讨论】:
-
std::vector<Foo> vec; vec.size() * 2,因为我们知道Foo::size总是返回 2。:)
标签: c++ stl containers member-functions