Czemu wynikiem jest 0 ? Jak dam cout<<buf w operator() to widzę że sumuje....
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
template<class T>
class Sum
{
T buf;
public:
Sum(): buf(T()) {}
void operator()(const T val)
{
buf += val;
}
friend ostream& operator<<(ostream& os, const Sum& s)
{
return os << s.buf;
}
};
int main()
{
vector<double> v;
for(int i=0;i<5;++i)
v.push_back(i);
Sum<double> s;
for_each(v.begin(), v.end(), s);
cout << s << '\n';
return 0;
}