Lets assume I call sort with a lambda like this, with nearPoints
being a vector of points in 3D.
sort(nearPoints.begin(), nearPoints.end(),
[srcPoint](const kd_point &A, const kd_point &B)
{return Distance(srcPoint, A) < Distance(srcPoint, B); });
In order to save on redundant calls to Distance
, I would like to pre-calculate the values, and have the call to sort use them.
The elegant solution is, IMHO, to prep a second vector with distances, read it in the lambda, and switch elements as necessary.
My problem is how to find A
's and B
's indexes in nearPoints
, so I would know which elements to access in the second vector. Is there a way to do it?
I know I can make a new vector with each elements containing a point and its distance from srcPoint
, but that would require populating the new vector before the sort, and extracting the points back in the right order after the sort, which strikes me as inelegant.