1

I am working on a code using divide and conquer. It just sums the elements of an array: EDIT: Code without inline lamda. Whay I want to is to use a binary operation op so that I can use any binary operation inside the code (not only sum). For example, passing an argument called op:

long reduce_rec(const long *A,long low,long high,myfunclambdahere op)
{
    long result;
    long n = high - low;
    if (n == 0){
      return 0;
    }

    else if(high - low == 1){
      return A[low];
    }
    else{
       long mid=(low+high)/2;
       long a,b;

       a = reduce_rec(A,low,mid);
       b = reduce_rec(A,mid,high);
       //result = a + b;
         result = myfunclambdahere(a,b);
    }
    return result;
}

Now, I want to use this function so that any operator op (binary operator) that is passed to my function can be used. I read that this can be done with a lambda function, but I am not so familiar with it in C++.

they give me the following template

template <class Assoc_comb_op>
value_type reduce_rec(Assoc_comb_op op, long *source, const value_type lo, const value_type hi) {:

But I dont know how to use lambdas and how to code this behavior. Can someone explain it to me?

Thanks in advance

5
  • then, how I can achieve this behavior? can you provide code for this? Commented Nov 24, 2014 at 1:57
  • Try adding template <typename myfunclambdahere> in front of your function definition. Commented Nov 24, 2014 at 4:29
  • Then somewhere else, define a lambda and pass it to your reduce function: auto mylambda = [x, y] (int x, int y) { return x + y;}; reduce_rec(data,low,high,mylambda);. Commented Nov 24, 2014 at 4:36
  • Don't forget to update your function implementation to pass the lambda to recursive calls as well. Commented Nov 24, 2014 at 4:37
  • didrec, you don't need to capture lambda parameters. Commented Nov 24, 2014 at 5:36

1 Answer 1

1

Lambda passed as parameter via std::function specification for type safety.

#include <functional>

long reduce_rec(const long *A, long low, long high,
                const std::function<long(long, long)>& op)
{
    long result;
    long n = high - low;
    if (n == 0){
      return 0;
    }

    else if(high - low == 1){
      return A[low];
    }
    else{
       long mid=(low+high)/2;
       long a,b;

       a = reduce_rec(A, low, mid, op);
       b = reduce_rec(A, mid, high, op);
       //result = a + b;
       result = op(a,b);    // lambda call here
    }
    return result;
}


int caller()
{
    auto lambda = [](long a, long b){ return (a + b) * (a - b); };

    return reduce_rec(nullptr /*your data*/, 10, 1110, lambda);
}
Sign up to request clarification or add additional context in comments.

1 Comment

thank you, so if I want to do max, then I use return max(a,b) inside the aut lambda funciton right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.