C++ (Qt)template <class It, class UnaryFunction>It qmax_element(It first, It last, UnaryFunction f){ auto max = f(*first); It it = first++; for (; first != last; ++first) { auto val = f(*first); if (val > max) { max = val; it = first; } } return it;}
C++ (Qt) std::vector<int> v = {21, 3434222, 3, -10, -4, 101, 5}; auto max = cost(v[0]); auto it = std::max_element(v.begin(), v.end(), [&max](int, int x)->bool { auto val = cost(x); bool res = val > max; if (res) max = val; return res; });
C++ (Qt) auto points_costs = ranges::view::transform(points, cost); auto index = ranges::distance(points_costs.begin(), ranges::max_element(points_costs));