C++ Logo

std-proposals

Advanced search

arg_min and arg_max algorithms

From: Jason Beach <jason.m.beach_at_[hidden]>
Date: Tue, 15 Dec 2020 19:59:51 -0500
Hi,
  I recently wrote a simple particle filter for a robotics project and
needed an arg_min function. This function returns the argument that
results in a minimum value of a function (as opposed to the minimum
argument value itself https://en.wikipedia.org/wiki/Arg_max). In my case,
from a list of points I needed to find the point that was closest to
another given point.

I based my implementation on the examples given on cppreference for
std::transform and std::min_element. The major advantage of a combined
algorithm as opposed to using std::transform and std::min_element
individually is that it cuts the number of function evaluations in half by
caching the result. A working implementation for arg_min is here:
https://godbolt.org/z/P58Yca. Arg_max is similar and overloads to
eliminate the begin/end iterators could be added (and probably ones to work
with ranges, although I have no experience with them yet).

I think this would be a valuable addition to the algorithms library as it
has fairly broad applicability. I'm curious what others think.

Thanks,
Jason

Received on 2020-12-15 19:00:31