11123 - Counting Trapizoid

All about problems in Volume 111. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

AxM
New poster
Posts: 5
Joined: Thu Oct 19, 2006 5:21 pm

Post 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?
misof
A great helper
Posts: 430
Joined: Wed Jun 09, 2004 1:31 pm

Post 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.
Post Reply

Return to “Volume 111 (11100-11199)”