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
template <typename myfunclambdahere>in front of your function definition.auto mylambda = [x, y] (int x, int y) { return x + y;}; reduce_rec(data,low,high,mylambda);.