Reputation: 137
I am trying to use transform_reduce to transform a set of numbers and then reduce it to the maximum of those elements.
auto lambda = [](uint64_t x) {
return function(x);
};
counting_iterator<uint64_t> start(1);
counting_iterator<uint64_t> finish(bound);
auto max_val = *transform_reduce(start, finish, lambda, std::max_element(start, finish));
My logic is that it will change all the values from 1 to bound to function(1) to function(bound), then with those values, give me the largest, but I am having issues, can anyone help me get to want I want to achieve?
Upvotes: 1
Views: 2146
Reputation: 218343
Signature of std::transform_reduce
(which takes one range) is:
template<class InputIt, class T, class BinaryOp, class UnaryOp>
T transform_reduce(InputIt first, InputIt last,
T init, BinaryOp binop, UnaryOp unary_op);
So, in your case:
auto max_val =
std::transform_reduce(start, finish,
lambda(*start),
[](const auto& lhs, const auto& rhs){ return std::max(lhs, rhs); },
lambda);
if legal (but it is not), [](const auto& lhs, const auto& rhs){ return std::max(lhs, rhs); }
could be replaced by using lambda_t = decltype(lambda(*start)); static_cast<const lambda_t & (*)(const lambda_t&, const lambda_t&)>(&std::max<lambda_t>)
as std::max<T>
would be ambiguous (might be max(const T&, const T&)
or max(std::initializer_list<T>)
), so the additional cast.
But anyway, we are not allowed to take address of most std
functions, including std::max
.
Upvotes: 2