I am trying to remove elements from a std::list
and keep some stats of deleted elements.
In order to do so, I use the remove_if function from the list, and I have a predicate. I would like to use this predicate to gather statistics. Here is the code for the predicate:
class TestPredicate
{
private:
int limit_;
public:
int sum;
int count;
TestPredicate(int limit) : limit_(limit), sum(0), count(0) {}
bool operator() (int value)
{
if (value >= limit_)
{
sum += value;
++count; // Part where I gather the stats
return true;
}
else
return false;
}
};
And here is the code for the algo:
std::list < int > container;
container.push_back(11);
TestPredicate pred(10);
container.remove_if(pred)
assert(pred.count == 1);
Unfortunately, the assertion is false because the predicate is passed by value. Is there a way to force it to be passed by reference ?