#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
class sum : unary_function<int, void> {
public:
argument_type sum;
sum() { sum = 0; }
result_type operator()(argument_type i) {
sum += i;
}
};
int main()
{
vector<int> v;
for(int i=1; i < 11; i++) v.push_back(i);
for(unsigned i=0; i < v.size(); ++i)
cout << v[i] << endl;
sum s;
s = for_each(v.begin(), v.end(), sum());
cout << "sum of v: " << s.sum << endl;
v[4]= 99;
s = for_each(v.begin(), v.end(), sum());
cout << "sum of v is now: " << s.sum;
return 0;
}
|