Is there a function similar to accummulate()
but provides a unary pre-condition to filter the linear container when performing the operation? I search for accummulate_if
but there isn't any. Thanks!
update:
Thanks for all the kind answers. I end up doing it this way:
std::for_each(v.begin(), v.end(), [&](int x){if (Pred) sum += x;});
Best Solution
Must you really use an algorithm? Something as simple as below won't do?
Sam's idea is also good. But I would do it with lambda: