c++ - identify item/class pointer after qsort -



c++ - identify item/class pointer after qsort -

first question please forgive naiveness here.

i'm diving triangulation library c++, sorts array of struct pointers before running it's triangulation method. i'm trying maintain track of 1 particular struct pointer (xyz) throughout app, updates according mouse position. problem is, whenever qsort method applied, pointer changes. how identify or maintain track of struct xyz pointer?

here 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 testing) 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 , update mouse methods.

mousepointer->x = mousex; mousepointer->y = mousey; // if don't qsort here reference fine, need to. qsort(allpointers, 2, sizeof(xyz), xyzcompare); // triangulate, etc

you have couple of options:

you search unique entry after sorting. if add together marker fellow member search linearly marker. if entry matching x/y coordinate other bsearch in sorted array. combine using bsearch find right x coordinate followed (shorter) linear search marker. you can add together layer of indirection. instead of sorting array of xyz structures, create parallel list of indexes or pointers array , sort xyz * or int references instead. mousepointer reference remain valid.

c++ pointers struct qsort

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -