Hello,
I'm trying to sort a vector of elements using the std::sort() algorithm, but the predicate I need isn't really straightforward, since it needs outside values.
The vector itself is just a list of ints, but they refer to other objects. These object id's have to be sorted by their relative distance to an external object.
The predicate should look something like this (but it obviously doesn't work):
1 2 3
|
bool distSort(int n, int i, int j) {
return (distance[n][i] < distance[n][j]);
}
|
with "n" being the external element being compared to. A different external object is used depending on the situation, so hardcoding it isn't an option.
I was thinking of making a support class with simply a member "n" and a methods "getDistance(i)" and "setN()", so I could call a predicate "return (class.getDistance(i) < class.getDistance(j))" and set the N value between sort calls, but this seems very unelegant. Is there a more logical way to do it?
Coding my own sort algorithm would very likely be very inefficient compared to the <algorithm> one, and since it's probably going to be called a few hundred times, I'd prefer using the premade version.
Any help would be greatly appreciated!