I have an array float points[keyCount][d]
. d
is a global variable, either 2 or 3. Additionally:
int axis = something // 0 or 1 -- depends on points stored in array
然后,我想使用类似以下内容的比较器对它们进行排序:
struct comp{
int dim;
comp(int dim){ this->dim = dim;}
bool operator()(float* a, float* b){
int otherDim = (dim + 1)%d;
if(a[dim] < b[dim])
return true;
if(a[dim] > b[dim])
return false;
return a[otherDim] < b[otherDim];
}
};
That is, use the variable axis
to see which coordinate axis should be used to sort the points.
问题:
I tried sort
function as:
sort(points, points + keyCount, comp(axis));
但这不起作用。
I also tried qsort
, but wasn't able to figure out how to use the axis
variable in that.
Also, note that I can't use vector
or pair
or array<>
. Just the "normal" arrays. Is it possible to do this using STL?