fork(1) download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <functional>
  4. #include <vector>
  5. #include <numeric>
  6. using namespace std::placeholders;
  7.  
  8. template<class T>
  9. struct multiply_range : public std::binary_function<T, T, T> {
  10. public:
  11. multiply_range() : i(0), j(1) { }
  12. T operator()(T a, T b) {
  13. /*if (!(this->i++ % 2)) {
  14.   if (this->j++ % 2)
  15.   return a * b;
  16.   } else {
  17.   this->j++;
  18.   return 0;
  19.   }*/
  20. return (!(this->i++ % 2)) ? ((this->j++ % 2) ? a * b : 0) : (this->j++, 0);
  21. }
  22. private:
  23. std::size_t i, j;
  24. };
  25.  
  26.  
  27. int main() {
  28. std::function<int(int, int)> Multiply = std::bind<int>(multiply_range<int>(), _1, _2);
  29. std::vector<int> vec_numsx = {1,2,3,4,5};
  30. std::vector<int> vec_numsy = {5,6,7,8,9,10};
  31. std::cout << std::inner_product(vec_numsx.begin(),
  32. vec_numsx.end(),
  33. std::next(vec_numsy.begin()),
  34. 0,
  35. std::plus<int>(),
  36. Multiply);
  37. return 0;
  38. }
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
80