Ç的qsort()与动态N按2多维数组 [英] C qsort() with dynamic n by 2 multi-dimensional array
问题描述
首先,我定义的2列和10列的动态数组。整数数量
设置为10这里只是举例来说。
First, I defined a dynamic array with 2 columns and 10 row. The integer number
is set to 10 here just for example.
int** array;
int number = 10;
array = malloc(number * sizeof(int*));
for (i = 0; i < number; i++)
array[i] = malloc(2 * sizeof(int));
然后我尝试使用的qsort()
就可以了。
qsort( array, number, sizeof array[0], compare );
这是我比较功能。它按在第一列中的整数值,则排序由第二塔同时preserving在第一列中的顺序。例如。 0 2,1 7,0 1将成为0 1 0 2 1 7
This is my compare function. It sorts by the integer values in the first column, then sorts by the second column while preserving the order in the first column. E.g. "0 2, 1 7, 0 1" will become "0 1, 0 2, 1 7".
int compare ( const void *pa, const void *pb ) {
int (*a)[1] = pa;
int (*b)[1] = pb;
if ( (a[0][0] < b[0][0]) || (a[0][0] == b[0][0])&&(a[1][0] < b[1][0]) ) return -1;
if ( (a[0][0] > b[0][0]) || (a[0][0] == b[0][0])&&(a[1][0] > b[1][0]) ) return +1;
return 0;
}
问
这曾与静态数组。我知道,因为我有一个动态数组,这是一个指针数组它现在无法正常工作。
This worked with a static array. I know it doesn't work now because I have a dynamic array, which is an array of pointers.
我怎样才能适应这个code与动态创建多维数组工作?
How can I adapt this code to work with the dynamically created multi-dimensional array?
推荐答案
样品code
#include <stdio.h>
#include <stdlib.h>
int compare ( const void *pa, const void *pb ) {
const int *a = *(const int **)pa;
const int *b = *(const int **)pb;
if(a[0] == b[0])
return a[1] - b[1];
else
return a[0] - b[0];
}
/*
#define NUMCMP(x,y) (((x) < (y)) ? -1 : ((x) > (y)) ? 1 : 0)
int compare ( const void *pa, const void *pb ) {
const int (*a)[2] = *(const int (**)[2])pa;
const int (*b)[2] = *(const int (**)[2])pb;
int tmp;
if((tmp=NUMCMP((*a)[0], (*b)[0]))==0)
return NUMCMP((*a)[1], (*b)[1]);
else
return tmp;
}
*/
int main(void){
int **array;
int number = 10;
int i;
array = malloc(number * sizeof(int*));
for (i = 0; i < number; i++){
array[i] = malloc(2 * sizeof(int));
array[i][0] = rand()%20;
array[i][1] = rand()%20;
}
for(i = 0;i < number;++i)
printf("%2d, %2d\n", array[i][0], array[i][1]);
printf("\n");
qsort(array, number, sizeof array[0], compare);
for(i = 0;i < number;++i)
printf("%2d, %2d\n", array[i][0], array[i][1]);
return 0;
}
什么 *(const int的**)PA
阵列= {为(int *),为(int *),为(int *),...,为(int *)}
array = {(int *), (int *), (int *), ... , (int *) }
的qsort需要元件每个元素地址(swap等因为尺寸和数量,并由于仅给定的信息启动该元件的地址)。
qsort need each element address for element (for swap, etc. Because the size and number and start address of the element since only the given information).
例如及(INT *),及(INT *)
E.g &(int *), &(int *)
所以(INT **)
传递给函数进行比较。
so (int **)
pass to function compare.
通话比较(INT **,** INT)
及(INT *)
意味着在ARG INT **
call compare(int **, int **)
&(int*)
meant at arg int**
比较功能prototypeis CMP(常量无效*,常量无效*)
compare function prototypeis cmp(const void*, const void*)
投(const int的**)PA
被转换为通过原来的指针。
cast (const int**)pa
is cast to passed original pointer.
*((const int的**)PA)
是解引用原始元素的指针(为int *
)
*((const int **)pa)
is dereference original element pointer(int*
)
这篇关于Ç的qsort()与动态N按2多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!