I have a class, with a function which returns a count, like this:
class CTestClass
{
public:
// ...
size_t GetCount()const;
// ...
};
And somewhere in my program I have a vector of objects of that class. I have a function to get the total count (sum of results of CTestClass::GetCount()), implemented like a normal loop:
size_t sum = 0;
for(vector<CTestClass>::const_iterator it = v.begin(); it != v.end(); ++it)
{
sum += it->GetCount();
}
I want to refactor it to use the facilities available in the standard library, and I thought of accumulate
. I've been able to do it by using a function object (easy), but I'm almost sure it can be done without declaring another object (I don't have C++11 or boost, so no lambdas, but I have TR1).
When looking for an answer, I've found these resources, but they don't solve the question:
accumulate
, plus
and
bind
, but uses a data member instead of a member function. So, is there a way to do this using bind, or something similar?
I think the following should work:
using namespace std;
using namespace std::tr1;
using namespace std::tr1::placeholders;
accumulate(v.begin(), v.end(), 0,
bind(plus<size_t>(), _1, bind(&C::GetCount, _2))
);
This works, too:
class CTestClass
{
public:
// ...
size_t GetCount()const;
private:
operator size_t() { return GetCount(); }
// ...
template <typename InputIterator, typename T>
friend T std::accumulate(InputIterator, InputIterator, T);
};
std::vector<CTestClass> v;
// fill vector
size_t result = std::accumulate(v.begin(), v.end(), 0);
No TR1, no bind :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With