S
Suman
I just can't get my head around to create a suitable
binary_function that will do this i.e. call a member function
from within accumulate. In fact I rolled my own template.
But I have a terrible feeling of reinventing the wheel.
/
*----------------------------------------------------------------------
*/
#include <vector>
#include <iterator>
#include <iostream>
using namespace std;
template <class I, class V, class Fn1, class Fn2>
V accumulate2(I first, I last, V val, Fn1 op, Fn2 memfn) {
for (; first != last; ++first)
val = op(val, memfn(*first));
return val;
}
struct strange {
strange(int a, int b) : _a(a), _b(b) {}
int value() { return _a + 10 * _b; }
int _a, _b;
};
int main() {
std::vector<strange> dv;
dv.push_back(strange(1, 3));
dv.push_back(strange(4, 6));
dv.push_back(strange(20, -11));
cout << accumulate2(dv.begin(), dv.end(),
0, std:lus<int>(),
mem_fun_ref(&strange::value))
<< endl;
}
/
*----------------------------------------------------------------------
*/
I have not yet looked into Boost -- but suggestions
are most welcome.
Regards,
Suman
binary_function that will do this i.e. call a member function
from within accumulate. In fact I rolled my own template.
But I have a terrible feeling of reinventing the wheel.
/
*----------------------------------------------------------------------
*/
#include <vector>
#include <iterator>
#include <iostream>
using namespace std;
template <class I, class V, class Fn1, class Fn2>
V accumulate2(I first, I last, V val, Fn1 op, Fn2 memfn) {
for (; first != last; ++first)
val = op(val, memfn(*first));
return val;
}
struct strange {
strange(int a, int b) : _a(a), _b(b) {}
int value() { return _a + 10 * _b; }
int _a, _b;
};
int main() {
std::vector<strange> dv;
dv.push_back(strange(1, 3));
dv.push_back(strange(4, 6));
dv.push_back(strange(20, -11));
cout << accumulate2(dv.begin(), dv.end(),
0, std:lus<int>(),
mem_fun_ref(&strange::value))
<< endl;
}
/
*----------------------------------------------------------------------
*/
I have not yet looked into Boost -- but suggestions
are most welcome.
Regards,
Suman