- UID
- 1023
- UCC
-
- 声望
-
- 好评
-
- 贡献
-
- 最后登录
- 1970-1-1
|
发表于 2008-12-23 19:40:59
|
显示全部楼层
- #include <cstdlib>
- #include <iostream>
- using namespace std;
- int values[] = { 40, 10, 100, 90, 20, 25 };
- int compare (const void * a, const void * b)
- {
- return ( *(int*)a - *(int*)b );
- }
- int main ()
- {
- int n;
- qsort (values, 6, sizeof(int), compare);
- for (n=0; n<6; n++) cout << values[n];
- return 0;
- }
复制代码
Qsort:
void qsort ( void * base, size_t num, size_t size, int ( * comparator ) ( const void *, const void * ) );
<cstdlib>
Sort elements of array
Sorts the num elements of the array pointed by base, each element size bytes long, using the comparator function to determine the order.
The sorting algorithm used by this function compares pairs of values by calling the specified comparator function with two pointers to elements of the array.
The function does not return any value, but modifies the content of the array pointed by base reordering its elements to the newly sorted order.
Parameters
base
Pointer to the first element of the array to be sorted.
num
Number of elements in the array pointed by base.
size
Size in bytes of each element in the array.
comparator
Function that compares two elements. The function shall follow this prototype:
int comparator ( const void * elem1, const void * elem2 );
The function must accept two parameters that are pointers to elements, type-casted as void*. These parameters should be cast back to some data type and be compared.
The return value of this function should represent whether elem1 is considered less than, equal, or grater than elem2 by returning, respectivelly, a negative value, zero or a positive value.
Return Value
(none) |
|