我有一个元素容器,每个元素都有它的size()
成员函数。我已经设法通过编写二进制操作来累积容器元素的总大小add_size
:
#include <algorithm>
#include <vector>
#include <functional>
#include <numeric>
#include <iostream>
class A
{
int size_ ;
public:
A ()
:
size_(0)
{}
A (int size)
:
size_(size)
{}
int size() const
{
return size_;
}
};
template<typename Type>
class add_element
{
Type t_;
public:
add_element(Type const & t)
:
t_(t)
{}
void operator()(Type & t)
{
t += t_;
}
};
int add_size (int i, const A& a)
{
return i+=a.size();
}
using namespace std;
int main(int argc, const char *argv[])
{
typedef vector<A> Vector;
Vector v;
v.push_back(A(10));
v.push_back(A(5));
v.push_back(A(7));
v.push_back(A(21));
v.push_back(A(2));
v.push_back(A(1));
int totalSize = accumulate(v.begin(), v.end(), 0, add_size);
std::cout << totalSize << endl;
return 0;
}
这给出了正确的输出:
46
我想要做到这一点,而不是仅仅为 size 成员函数定义二进制操作add_size
,而是使用 mem_fun 和 binders。我怎样才能做到这一点?我怎样才能有效地做到这一点?我一开始add_element
就卡住了。
我需要在 C++03 中工作的解决方案。