#include <vector> #include <boost/lambda/lambda.hpp> #include <boost/lambda/if.hpp> // maybe more? void f1() { namespace lm = boost::lambda; int arr[] = {5, 1, 6, 10}; const size_t N = sizeof(arr) / sizeof(arr[0]); std::vector<int> v(arr, arr + N); // классический C++ вариант struct div5_functor : public std::unary_function<int, int> { int operator()(int val, int it) const { return val + ((it % 5) == 0 ? it : 0); } }; int sum = std::accumulate(v.begin(), v.end(), 0, div5_functor()); // вариант с использованием Boost Lambda Library sum = std::accumulate(v.begin(), v.end(), 0, lm::_1 + lm::if_then_else_return(lm::_2 % 5 == 0, lm::_2, 0)); printf("sum=%d\n", sum); } |