Mar 5, 2012 at 3:16pm Mar 5, 2012 at 3:16pm UTC
Wait, if you're using C++, why are you using qsort() when you can use std::sort()?
You'll probably want to write a function that takes two Cables, gets the costs from them, and return the order in which they (the elements) should go in the form of a boolean. std::sort() can take a function to replace the < operator (if it exists).
http://cplusplus.com/reference/algorithm/sort/
Good luck!
-Albatross
Last edited on Mar 5, 2012 at 7:29pm Mar 5, 2012 at 7:29pm UTC
Mar 5, 2012 at 3:21pm Mar 5, 2012 at 3:21pm UTC
Thanks for this. It doesn't really matter what i use, but i'm having trouble constructing the comparator, and would like some guidance on building that
Mar 5, 2012 at 3:24pm Mar 5, 2012 at 3:24pm UTC
Hmm... does line 7 of the example on that page give you a rough idea of what to do?
-Albatross
Mar 5, 2012 at 3:26pm Mar 5, 2012 at 3:26pm UTC
it does, but how would i specify that i want it to sort the array based on the cost element?
Mar 5, 2012 at 3:56pm Mar 5, 2012 at 3:56pm UTC
Since your cost element is public, then can't you just use the . operator to get it from your objects and compare that?
-Albatross
Mar 5, 2012 at 6:04pm Mar 5, 2012 at 6:04pm UTC
I'm just having trouble implementing it. For the comparator, it needs to follow int comparator ( const void * elem1, const void * elem2 );
I just don't know how to construct this to take the .cost element.
Mar 5, 2012 at 6:09pm Mar 5, 2012 at 6:09pm UTC
If you are using qsort you need to cast the pointers to the correct type before you can access the class members.
It's much easier to use std::sort, you don't need to cast. Just define the comparator
bool comparator ( const Cable& elem1, const Cable& elem2 );
Last edited on Mar 5, 2012 at 6:09pm Mar 5, 2012 at 6:09pm UTC
Mar 5, 2012 at 6:51pm Mar 5, 2012 at 6:51pm UTC
For simplicity's sake perhaps just use references to Cable objects?
1 2 3 4
int maxCable( const Cable &c1, const Cable &c2)
{
return (c1.cost < c2.cost) ? -1 : ((c1.cost > c2.cost) ? 1 : 0);
}
EDIT: After Peter87's explanation, reworked the return
Last edited on Mar 5, 2012 at 7:27pm Mar 5, 2012 at 7:27pm UTC
Mar 5, 2012 at 7:17pm Mar 5, 2012 at 7:17pm UTC
The problem is that the qsort comparator should return a negative number if elem1 is less than elem2, zero if both elements are equal and a positive number if elem1 is greater than elem2.
Mar 5, 2012 at 7:48pm Mar 5, 2012 at 7:48pm UTC
Texan40, with qsort, the comparator has to be
int comparator(const void *, const void *)
If it was just a regular comparator i'd be fine, but i can't seem to get this one to work.
I've currently got
1 2 3 4 5 6 7 8
int comparator ( const void * elem1, const void * elem2 )
{
Cable* c1= (Cable*) &elem1;
Cable* c2= (Cable*) &elem2;
if (c1->cost < c2->cost) return -1;
else if (c1->cost > c2->cost) return 1;
else return 0;
}
and
qsort(cables,ncables, sizeof (Cable), comparator);
Last edited on Mar 5, 2012 at 7:48pm Mar 5, 2012 at 7:48pm UTC
Mar 5, 2012 at 8:22pm Mar 5, 2012 at 8:22pm UTC
&elem1
this gives you a pointer to a void pointer.
Mar 5, 2012 at 8:29pm Mar 5, 2012 at 8:29pm UTC
Oh, I can't believe i didn't change that. Thanks, Peter87!