Page 2 of 2

Posted: Thu Oct 19, 2006 5:28 pm
by AxM
I have all the slopes in an STRUCT, so I want to sort them.. but I think that an n^2 algorithm is not the answer, so how can I sort an struct using qsort C++? or.. If you have another idea?

Posted: Thu Oct 19, 2006 7:05 pm
by misof
the qsort() function from <stdlib.h> is C, not C++
the C++ way is using sort() from <algorithm>

To use any of them, you just need a custom comparison function.
For C:

Code: Select all

// warning, not tested
int cmp(const void *aa, const void *bb) {
  struct myStruct *a = (struct myStruct *)aa;
  struct myStruct *b = (struct myStruct *)bb;
  // use aa->member, bb->member
  // return -1, 0 or 1
}
See the documentation for more info.