【发布时间】:2019-04-15 16:13:41
【问题描述】:
我在列表中保存了一些带有 x-y 坐标的二维点。我有一个方法,它根据点与光标的距离对数组进行排序,该方法将指针返回到离光标最近的点。
但是我使用 &points.first() 并且它总是指向列表的第一个元素。但是,在我使用列表后指针会发生变化。如何获得指向特定 ELEMENT 的指针,而不是列表的第一个元素。
我试过了: &points.first()
QList<Point2> points;
Point2 *DrawingWidget::closestToCursor(){
// Current mouse position
Point2 pos(m_x, m_y);
// There are no points
if(points.isEmpty()){
return NULL;
}
// Sorts according to distance to the cursor
std::sort(std::begin(points), std::end(points), [&pos](Point2 a, Point2 b) {
return pos.distanceFrom(a) < pos.distanceFrom(b);
});
// We dont allow points closer than 50px appart
if(pos.distanceFrom(points.first()) > 50){
return NULL;
}
// Even after the resort, this always points to the first element of the vector. How do I get this elements pointer instead?
// Currently it seems that the pointer is basically LIST+0x0, however if the element shifts to whatever position, how do I still have its pointer?
return &points.first();
}
每次我在新点附近调用此方法时,指针都会移动到列表的第一个元素,这是它应该做的,我知道这一点。但是我该怎么做呢?
【问题讨论】:
-
不要忘记,C++ 强烈鼓励使用
nullptr而不是NULL。 -
如果不是绝对必要,不要使用列表。他们即使不是邪恶的,也是地狱般的。
-
列表中的元素就是元素。容器存储值而不是引用,因此如果您希望您的对象在列表之外有一些身份,那么您可以在列表中存储指针,但这通常不是一个好主意,所以最好使用答案中提出的内容跨度>
标签: c++ list pointers vector std