This is C++.
I am trying to write a C++ STL function find_min_if.
template<typename Iter, typename BinaryPredicate> Iter find_min_if(Iter it, Iter end, BinaryPredicate funct){ if (it == end){ return end; } auto min = it; for (; it < end; ++it){ if (funct(it, min)){ min = it; } } return min; }
The idea is I will have an array of pair of ints and I compare each pair by the sum of its values. How can I write a lambda to compare those pairs?
bool binaryPredicate(const std::pair<int, int>& p1, const std::pair<int, int>& p2){ return (p1.first + p1.second) < (p2.first + p2.second);
}
auto min = my::min_element_when(std::begin(v1), std::end(v1), binaryPredicate);
https://stackoverflow.com/questions/67238711/how-to-use-find-min-using-binarypredicate April 24, 2021 at 10:06AM
没有评论:
发表评论