【问题标题】:Sorting std::list of pointers to objects with comparator使用比较器对指向对象的指针列表进行排序
【发布时间】:2014-04-12 01:57:11
【问题描述】:

我有一个类,它有指向对象的指针列表,这些对象需要使用作为该类成员的比较器进行排序:

class VoronoiDiagram
{
public:
    void BuildVoronoi();

private:
    list<shared_ptr<QPointF>> inputPoints
    QPointF currentVoronoiPoint;

    bool compairingPointsPredictate(shared_ptr<QPointF> a, shared_ptr<QPointF> b) {    float d1 = (a->x()-currentVoronoiPoint.x())*(a->x()-currentVoronoiPoint.x()) + (a->y()-currentVoronoiPoint.y())*(a->y()-currentVoronoiPoint.y());
                                                                                float d2 = (b->x()-currentVoronoiPoint.x())*(b->x()-currentVoronoiPoint.x()) + (b->y()-currentVoronoiPoint.y())*(b->y()-currentVoronoiPoint.y());
                                                                                return d1 < d2; };          
};

我在 BuildVoronoi() 中调用这个:

inputPoints.sort(compairingPointsPredictate);

但这给了我两个错误:C3867 与我的比较器函数和 C2660 与 std::list::sort (不接收 1 个参数)。

对不起我的英语或解释。我真的希望这里有人可以帮助我。谢谢。

【问题讨论】:

  • 问题是成员函数有一个隐含的this 的第一个参数,所以它与std::list::sort 所需的签名不匹配。那里有很多重复项。
  • 那么,你能告诉我解决这个问题的方法吗?

标签: c++ qt list sorting


【解决方案1】:

sort 需要两个参数的比较函数,但声明为成员函数会导致调用类似于:

a.compairingPointsPredicate( b, c)
//                     ^
//                   no 't', it is predicate not predictate ;p

没有理由让 compairingPointsPredicate 成为类的成员。你可以让它成为一个独立的函数(如果需要访问私人数据,它是 QPointF 的朋友)。

class QPointF{

    friend bool compairingPointsPredicate(
                        shared_ptr<QPointF> a, shared_ptr<QPointF> b);          
};


bool compairingPointsPredicate(shared_ptr<QPointF> a, 
                                          shared_ptr<QPointF> b) {    
                                            float d1 = (a->x()-currentVoronoiPoint.x())
                                            *(a->x()-currentVoronoiPoint.x()) + 
                                            (a->y()-currentVoronoiPoint.y())
                                            *(a->y()-currentVoronoiPoint.y());
                                            float d2 = (b->x()-currentVoronoiPoint.x())
                                            *(b->x()-currentVoronoiPoint.x()) +
                                            (b->y()-currentVoronoiPoint.y())
                                            *(b->y()-currentVoronoiPoint.y());
                                            return d1 < d2;
}

用法:

inputPoints.sort(compairingPointsPredicate);

【讨论】:

  • 您能否给我一个示例链接,说明如何通过访问私有数据创建友好功能以及必须如何实现比较器?
  • 嗯...我试过了,但现在比较PointsPredictate“看不到”currentVoronoiPoint。
  • compairingPointsPredictate 必须有权访问 a->x() 及其 currentVoronoiPoint.x()。这是另一个与以下无关的问题:错误 std::list::sort (do not receive 1 argument)
  • 什么样的问题?我不明白:友好的函数必须可以访问 currentVoronoiPoint,但 VC2010 只会给我 C2065 和 C2228 错误与 currentVoronoiPoint。但主要问题是固定的,我猜。
  • @VIRUS 比较器必须是 QPointF 的朋友
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-11
  • 2011-02-03
  • 2014-07-05
  • 2020-12-04
  • 2018-06-07
  • 2020-09-17
相关资源
最近更新 更多