方法1:static bool compare(const QPointF &point_one, const QPointF &point_second)
{
return point_one.x() < point_second.x();
}
方法2:bool compare(const QPointF &point_one, const QPointF &point_second)
{
return point_one.x() < point_second.x();
}
问题一:在自己写的函数 使用自定义的排序方式时候,比如
//错误写法
QPointF myFunction(const QVector<QPointF>& points, const qreal xValue)
{
qSort(points.begin(), points.end(), compare);
}
原因是使用const关键字定义了常量,在后续的排序中并不能允许更改,但这种写法在qSort中进行了排序更改,所以报错。
解决方案
1.复制一份数据进行排序
QVector<QPointF> test_pnt(points);
qSort(test_pnt.begin(), test_pnt.end(), compare);
2.取消使用const关键字
问题2:在qSort中无法使用自定义的比较方式
解决方法:
需要在compare比较函数上使用static关键字进行修饰