I am trying to combine std::accumulate
with std::min
. Something like this (won't compile):
vector<int> V{2,1,3};
cout << accumulate(V.begin()+1, V.end(), V.front(), std::min<int>);
Is it possible?
Is it possible to do without writing wrapper functor for std::min
?
I know that I can do this with lambdas:
vector<int> V{2,1,3};
cout << std::accumulate(
V.begin()+1, V.end(),
V.front(),
[](int a,int b){ return min(a,b);}
);
And I know there is std::min_element
. I am not trying to find min element, I need to combine std::accumulate
with std::min
(or ::min
) for my library which allows function-programming like expressions in C++.
The problem is that there are several overloads of the
min
function:Therefore, your code is ambiguous, the compiler does not know which overload to choose. You can state which one you want by using an intermediate function pointer:
Very old question, but may help some one else :) Find minimum accumulating sum.
C++14: