小编nob*_*ant的帖子

使用qsort同时对两个数组进行排序?

我可以对单词指针数组进行排序,使它们按字母顺序排序,问题是我需要对整数数组(特定单词的使用次数)进行排序,以便整数与它们的位置相同各自的话:

我的代码:

for (i = 0; i < numWords; i++) {
    // prints out the words and their frequency respectively
    printf("%s - %d\n", dictionary[i], frequency[i]); 
}

//sorts the dictionary so that the words are 'alphabetical'
qsort(dictionary, numWords, sizeof(char *), rstrcmp);  
printf("\nafter qsort\n");  //checkmark

for (i = 0; i < numWords; i++) {
    // prints the word list alphabetically, but the frequencies are no longer matched
    printf("%s - %d\n", dictionary[i], frequency[i]); 
}
Run Code Online (Sandbox Code Playgroud)

...比较功能V.

int rstrcmp(const void *p1, const void *p2) { …
Run Code Online (Sandbox Code Playgroud)

c arrays qsort

3
推荐指数
1
解决办法
2263
查看次数

标签 统计

arrays ×1

c ×1

qsort ×1