class DPoint
{
public:
DPoint(double _x,double _y, double _z)
: x(_x),y(_y),z(_z){}
double x;
double y;
double z;
};
class PointAverage: public binary_function<DPoint, DPoint, DPoint>
{
public:
PointAverage():xSum(0),ySum(0),zSum(0),numPoints(0){}
const DPoint operator()(const DPoint& avgSoFar, const DPoint& p)
{
++numPoints;
xSum += p.x;
ySum += p.y;
zSum += p.z;
return DPoint(xSum/numPoints,ySum/numPoints,zSum/numPoints);
}
private:
double xSum;
double ySum;
double zSum;
size_t numPoints;
};
int main()
{
list<double> ld;
ld.push_back(1.0);
ld.push_back(2.0);
ld.push_back(3.0);
ld.push_back(4.0);
double sum = accumulate(ld.begin(),ld.end(),0.0);
cout<<sum<<endl;
double product = accumulate(ld.begin(),ld.end(),1.0,multiplies<double>());
cout<<product<<endl;
vector<DPoint> pv;
pv.push_back(DPoint(2,3,5));
pv.push_back(DPoint(3,4,5));
pv.push_back(DPoint(5,6,2));
DPoint avg = accumulate(pv.begin(),pv.end(),DPoint(0,0,0),PointAverage());
cout<<avg.x<<" "<<avg.y<<" "<<avg.z<<endl;
}