T
thomas
Hi,
I have found one interesting thing.
the qsort() for c:
--------------c----
int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b ); -----(1)
}
qsort (values, 6, sizeof(int), compare);
-------------c++----
bool cmp(const int &a, const int &b){
return a<b; -----(2)
}
sort(values, values+6, cmp);
--------------------
We find that both sort the "values" array in ascending order, the first
(1) uses a converse comparison results with (2).
It says that if "compare" returns positive, we should put "a"
backwords for qsort; but if "cmp" returns true(like positive), we
should put "a" in front for sort.
I have found one interesting thing.
the qsort() for c:
--------------c----
int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b ); -----(1)
}
qsort (values, 6, sizeof(int), compare);
-------------c++----
bool cmp(const int &a, const int &b){
return a<b; -----(2)
}
sort(values, values+6, cmp);
--------------------
We find that both sort the "values" array in ascending order, the first
(1) uses a converse comparison results with (2).
It says that if "compare" returns positive, we should put "a"
backwords for qsort; but if "cmp" returns true(like positive), we
should put "a" in front for sort.