I have a class with a function that returns a counter, for example:
class CTestClass { public:
And somewhere in my program, I have a vector of objects of this class. I have a function to get the total score (the sum of the results of CTestClass :: GetCount ()), implemented as a regular loop:
size_t sum = 0; for(vector<CTestClass>::const_iterator it = v.begin(); it != v.end(); ++it) { sum += it->GetCount(); }
I want to reorganize it to use the facilities available in the standard library, and I thought of accumulate
. I was able to do this using the function object (easily), but I'm pretty sure that it can be done without declaring another object (I don't have C ++ 11 or boost, so no lambdas, but I have TR1).
When you are looking for an answer, I found these resources, but they do not solve the question:
- This is almost the same question, and the answers provided are a loop, accumulate and functor, accumulate and lambda, but there is no answer to the binding, etc.
- This answer to a similar question uses
accumulate
, plus
and bind
, but uses a data item instead of a member function.
So, is there a way to do this with bind or something similar?
source share