qsort后识别项目/类指针

发布于 2024-11-19 09:18:15 字数 1002 浏览 5 评论 0原文

第一个问题所以请原谅我的天真。

我正在深入研究 C++ 的三角测量库,它在运行三角测量方法之前对结构指针数组进行排序。我试图在整个应用程序中跟踪一个特定的结构指针(XYZ),该指针根据鼠标位置进行更新。问题是,每当应用 qsort 方法时,此指针就会发生变化。如何识别或跟踪这个 struct XYZ 指针?

这是结构 &排序...

struct XYZ{
  double x, y, z;
};

int XYZCompare(const void *v1, const void *v2){
  XYZ *p1, *p2;

  p1 = (XYZ*)v1;
  p2 = (XYZ*)v2;
  if(p1->x < p2->x)
    return(-1);
  else if(p1->x > p2->x)
         return(1);
       else
         return(0);
}

带有鼠标指针引用的 XYZ 结构数组(此处有 2 个用于测试)...

XYZ *allPointers = new XYZ[100];
allPointers[0].x = 100;
allPointers[0].y = 200;
allPointers[0].z = 0;
allPointers[1].x = 50;
allPointers[1].y = 80;
allPointers[1].z = 0;
XYZ *mousePointer = &allPointers[0];

排序和更新鼠标方法。

mousePointer->x = mouseX;
mousePointer->y = mouseY;

// If I don't qsort here the reference is fine, but I need to.
qsort(allPointers, 2, sizeof(XYZ), XYZCompare); 
// triangulate, etc

first question so please forgive my naiveness here.

I'm diving into a triangulation library for c++, which sorts an array of struct pointers before running it's triangulation method. I'm trying to keep track of one particular struct pointer (XYZ) throughout my app, which updates according to the mouse position. Problem is, whenever the qsort method is applied, this pointer changes. How do I identify or keep track of this struct XYZ pointer?

Here is the struct & sort...

struct XYZ{
  double x, y, z;
};

int XYZCompare(const void *v1, const void *v2){
  XYZ *p1, *p2;

  p1 = (XYZ*)v1;
  p2 = (XYZ*)v2;
  if(p1->x < p2->x)
    return(-1);
  else if(p1->x > p2->x)
         return(1);
       else
         return(0);
}

The array of XYZ structs (2 here for testing) with mouse pointer reference...

XYZ *allPointers = new XYZ[100];
allPointers[0].x = 100;
allPointers[0].y = 200;
allPointers[0].z = 0;
allPointers[1].x = 50;
allPointers[1].y = 80;
allPointers[1].z = 0;
XYZ *mousePointer = &allPointers[0];

Sort and update mouse methods.

mousePointer->x = mouseX;
mousePointer->y = mouseY;

// If I don't qsort here the reference is fine, but I need to.
qsort(allPointers, 2, sizeof(XYZ), XYZCompare); 
// triangulate, etc

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

欢你一世 2024-11-26 09:18:15

您有几个选择:

  • 您可以在排序后搜索您的唯一条目。如果添加标记成员,则线性搜索标记。如果任何具有匹配 X/Y 坐标的条目与其他任何条目一样好,您可以在排序数组中bsearch 查找它。您可以通过使用 bsearch 来查找正确的 X 坐标,然后对标记进行(较短的)线性搜索,从而将这些组合起来。
  • 您可以添加一个间接层。不要对 XYZ 结构数组进行排序,而是在该数组中创建索引或指针的并行列表,并对 XYZ *int 引用进行排序。您的 mousePointer 引用仍然有效。

You have a couple of options:

  • You could search for your unique entry after sorting. If you add a marker member search linearly for the marker. If any entry with a matching X/Y coordinate is as good as any other you could bsearch for it in the sorted array. You could combine those by using bsearch to find the right X coordinate followed by a (shorter) linear search for the marker.
  • You can add a layer of indirection. Instead of sorting your array of XYZ structures, create a parallel list of indexes or pointers into that array and sort the XYZ * or int references instead. Your mousePointer reference will remain valid.
糖果控 2024-11-26 09:18:15

qsort() 的第三个参数不应该是 size(XYZ*) 吗?您正在对指针进行排序,而不是对所指向的对象进行排序。

Shouldn't the third arg to qsort() be size(XYZ*)? You're sorting the pointers and not the objects being pointed to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文