Есть ли "accummulate_if"?

Есть ли функция, похожая на accummulate(), но обеспечивает унарное предварительное условие для фильтрации линейного контейнера при выполнении операции? Я ищу accummulate_if, но его нет. Спасибо!

обновление: Спасибо за все добрые ответы. Я делаю это так:

std::for_each(v.begin(), v.end(), [&](int x){if (Pred) sum += x;});

Ответы

Ответ 1

Передайте свой собственный двоичный код для std:: accumulate():

#include <iostream>
#include <vector>
#include <numeric>

bool meets_criteria(int value) {
    return value >= 5;
}

int my_conditional_binary_op(int a, int b) {
    return meets_criteria(b) ? a + b : a;
}

class my_function_object {
private:
    int threshold;
public:
    my_function_object(int threshold) :
            threshold(threshold) {
    }

    bool meets_criteria(int value) const {
        return value >= threshold;
    }

    int operator()(int a, int b) const {
        return meets_criteria(b) ? a + b : a;
    }
};

int main() {
    std::vector<int> v { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

    //sum [5...10] = 45
    int sum;

    sum = std::accumulate(v.begin(), v.end(), 0, my_conditional_binary_op);
    std::cout << sum << std::endl;

    //Use a function object to maintain states and additional parameters like 'threshold'
    sum = std::accumulate(v.begin(), v.end(), 0, my_function_object(5));
    std::cout << sum << std::endl;

    return sum;
}

Ответ 2

Действительно ли вы должны использовать алгоритм? Что-то такое же простое, как ниже, не будет?

for (const auto& v: V)  if(pred(v)) sum+=v;

Идея Сэма тоже хороша. Но я бы сделал это с лямбдой:

 sum = accumulate(
     V.begin(), V.end(), 0, 
     [](int a, int b){return pred(b)? a+b: a;}
 );   

Ответ 3

Нет, но вы можете написать :

template
  <
  typename InputIterator,
  typename AccumulateType,
  typename BinaryOperation,
  typename Predicate
  >
const AccumulateType accumulate_if(
  InputIterator first,
  const InputIterator last,
  AccumulateType init,
  BinaryOperation&& binary_op,
  Predicate&& predicate)
{
  for (; first != last; ++first)
    if (predicate(*first)) init = binary_op(init, *first);
  return init;
}

Использование:

int main(int argc, char* argv[])
{
    std::vector<int> v = {1,2,3,4,5};
    std::cout << accumulate_if(v.begin(), v.end(), 0, std::plus<int>(), [] (int n) { return n > 3; });
    return 0;
} // outputs 9