programming-examples/c++/Others/A function object that computes an integer sum..cpp

35 lines
596 B
C++
Raw Normal View History

2019-11-15 12:59:38 +01:00
A function object that computes an integer sum.
#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;
return 0;
}