[英]qsort for sorting an array of objects
I'm trying to use the qsort() to sort an array of object pointers (PointP = Point*) attached is the compare function and the sorting, The problem is that nothing happens and sorting doesn't occur. 我正在尝试使用qsort()来排序一个对象指针数组(PointP = Point *)附加的是比较函数和排序,问题是没有任何反应并且不会发生排序。
int compareByAngleP(const void* elem1,const void* elem2) {
PointP point1 = (PointP) elem1;
PointP point2 = (PointP) elem2;
if (abs(point1->getAngle() - point2->getAngle())>0.001)
{
return point1->getAngle() - point2->getAngle();
}
return point1->getY() - point2->getY();
}
void sortArrayP(PointP* array, int size) {
qsort(array,size, sizeof(PointP), compareByAngleP);
}
My advice would be to forget about std::qsort
, favour plain old std::sort
. 我的建议是忘记std::qsort
,赞成普通的旧std::sort
。 Unlike std::qsort
it is typesafe and in most implementations much faster. 与std::qsort
不同,它是类型安全的,在大多数实现中要快得多。
std::sort(array, array+size, compareByAngleP);
And do rid of the void*
in the compare function in favour of actual types. 并且在比较函数中除去void*
以支持实际类型。
Furthermore, if you are using C++11 and the array is local simply: 此外,如果您使用的是C ++ 11,并且该数组只是本地的:
std::sort(std::begin(array), std::end(array), compareByAngleP);
Or even better just use a std::vector
or other most approitaite container. 或者甚至更好地使用std::vector
或其他最受欢迎的容器。
std::vector<Point> array { ... };
std::sort(array.begin(), array.end(), compareByAngleP);
You may need to amend your comparison function so that it returns true if the first argument is less than the second. 您可能需要修改比较函数,以便在第一个参数小于第二个参数时返回true。 (or simply implement operator <
for Point). (或简单地实现operator <
for Point)。
http://en.cppreference.com/w/cpp/algorithm/sort http://en.cppreference.com/w/cpp/algorithm/sort
http://en.cppreference.com/w/cpp/container/vector http://en.cppreference.com/w/cpp/container/vector
There are several problems in your use of qsort
and in your comparison function. 在使用qsort
和比较函数时有几个问题。
qsort
will use memcpy
(or similar) to re-order the elements of the array. qsort
将使用memcpy
(或类似)来重新排序数组的元素。 If your Point
type has a copy-constructor (or a member that has one), then using qsort
on it is asking for trouble (it results in Undefined Behaviour). 如果您的Point
类型具有复制构造函数(或具有一个复制构造函数的成员),那么在其上使用qsort
会产生问题(导致未定义的行为)。
Your compareByAngleP
function is not suitable for sorting, because the relation it checks is not transitive. 您的compareByAngleP
函数不适合排序,因为它检查的关系不是传递的。 For example, if you have three points A
, B
and C
, all with the same Y
coordinate and respectively with the angles 10.000
, 10.001
and 10.002
, then compareByAngeP
will indicate that A == B
and B == C
, but A != C
, which can cause havoc for a sorting function. 例如,如果您有三点A
, B
和C
,都具有相同的Y
与协调的角度分别10.000
, 10.001
和10.002
,然后compareByAngeP
将表明A == B
和B == C
,但A != C
,可能会对排序功能造成严重破坏。
Following qsort reference (http://en.cppreference.com/w/cpp/algorithm/qsort) 3rd parameter of qsort should be 'size of each element in the array in bytes' so instead 在qsort引用之后(http://en.cppreference.com/w/cpp/algorithm/qsort)qsort的第三个参数应该是'数组中每个元素的大小,以字节为单位',而是
qsort(array,size, sizeof(PointP), compareByAngleP);
try 尝试
qsort(array,size, sizeof(PointP*), compareByAngleP);
Also you are implicity casting float to int (while returning value from comparing function). 另外,你是将imp浮动到int(同时从比较函数返回值)。 But: 但:
(int)0.2 == 0
我想如果你从比较函数返回bool而不是int,那么会发生一些不同的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.