qsort声明为
void qsort (void* base, size_t num, size_t size,
int (*compar)(const void*,const void*));
Run Code Online (Sandbox Code Playgroud)
我想知道qsort如何实现反射属性.我的意思是它如何调用我们传递的名字的函数?
我需要排序一个指向struc的指针数组.实际上,我需要在地址之间进行搜索,以查看数组中是否存在指向结构的指定指针.不幸的是,我在这些结构中没有任何"可比性"的东西,所以我只想按地址排序.我的代码是这样的:
item* arr[SIZE];
//something is inserted
qsort(arr, SIZE, sizeof(item*), (void*)compare_funct);
//CUT
bsearch(curr, arr, SIZE, sizeof(item*), (void*)compare_funct);
Run Code Online (Sandbox Code Playgroud)
我尝试创建一个compare_funct,只是将指针转换为int并返回它们的区别,但它似乎不起作用.特别是,当我进行bsearch时,即使我知道元素包含在数组中,我总是得到一个NULL作为返回值.
我试图通过char对结构运行数组进行排序,但是当我打印数组时,没有任何内容被排序.看看这个:
struct run {
char name[20], weekday[4], month[10];
(And some more...)
};
typedef struct run run;
int name_compare(const void *a, const void *b)
{
run *run1 = *(run **)a;
run *run2 = *(run **)b;
return strcmp(run1->name, run2->name);
}
int count_number_of_different_persons(run results[])
{
int i = 0;
qsort(results, sizeof(results) / sizeof(run), sizeof(run), name_compare);
for(i = 0; i <= 999; i++)
{
printf("%s\n", results[i].name);
}
// not done with this function yet, just return 0
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上面的输出只是按照最初放置的顺序列出的名称
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int sortstring(const void *str1, const void *str2) {
const char *rec1 = str1;
const char *rec2 = str2;
}
void sortutil(char* lines[]) {
qsort(lines, 200, sizeof(char), sortstring);
}
Run Code Online (Sandbox Code Playgroud)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "sortutil.h"
int getarray(char *lines[]) {
int i = 0;
char *text = (char *)malloc(200);
while (fgets(text, 200, stdin) != NULL) {
lines[i] = text;
i++;
text = (char *)malloc(200);
}
return i;
}
void printarray(char *lines[], int max) { …Run Code Online (Sandbox Code Playgroud) 谁能告诉我以下示例有什么问题?我把它从这里拿走并换成int了unsigned long.我也改变了cmpfunc正确处理unsigned long.
#include <stdio.h>
#include <stdlib.h>
unsigned long values[] = { 88, 56, 100, 2, 25 };
int cmpfunc (const void * a, const void * b)
{
if(*(unsigned long*)a - *(unsigned long*)b < 0){
return -1;
}
if(*(unsigned long*)a - *(unsigned long*)b > 0){
return 1;
}
if(*(unsigned long*)a - *(unsigned long*)b == 0){
return 0;
}
}
int main()
{
int n;
printf("Before sorting the list is: …Run Code Online (Sandbox Code Playgroud) 所以,我有一个比较两个const void*指针的函数,如果它有更大的地址,则一个指针大于其他指针
int func (const void* a, const void* b)
{
return (int)((long)(a) - (long)(b));
}
Run Code Online (Sandbox Code Playgroud)
我有一个void*数组,array [0]比array [1]大
void* array[2];
void* a = malloc(10);
void* b = malloc(10);
if (func(a, b) < 0)
{
array[0] = b;
array[1] = a;
}
else
{
array[0] = a;
array[1] = b;
}
// for example, array contains 0x15cfeb0 and 0x15cfe90
Run Code Online (Sandbox Code Playgroud)
之后我正在进行qsort,阵列不会改变!
qsort(array, 2, sizeof(void*), (*func));
// array is 0x15cfeb0 and 0x15cfe90 instead of expected 0x15cfe90 and 0x15cfeb0
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
根据文档 qsort 应该像这样使用:
std::qsort(a, size, sizeof *a, [](const void* a, const void* b)
{
if(*a < *b) return -1;
if(*a> *a) return 1;
return 0;
});
Run Code Online (Sandbox Code Playgroud)
如果值相同并且我不介意哪个先出现,或者我更喜欢基于其他变量的一个在另一个之前,我可以这样做吗?
if(*a < *b) return -1;
else return 1;
Run Code Online (Sandbox Code Playgroud) 我正在努力整合代码库(将qsort compar函数移动到新的标头/库,以便可以在不复制/意大利面的情况下共享它),并注意到在此过程中出现了一些奇怪的情况。
这是一个示范性清单:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
/** One record has three fields.
* Each field contains a NULL terminated string of length at most 7 characters. */
typedef char Record[3][8];
int main(void)
{
Record database[5] = {0};
strcpy(database[0][0], "ZING");
strcpy(database[0][1], "BOP");
strcpy(database[0][2], "POW");
strcpy(database[1][0], "FIDDLY");
strcpy(database[1][1], "ECHO");
strcpy(database[1][2], "ZOOOM");
strcpy(database[2][0], "AH");
strcpy(database[2][1], "AAAAA");
strcpy(database[2][2], "AH");
strcpy(database[3][0], "BO");
strcpy(database[3][1], "DELTA");
strcpy(database[3][2], "FO");
strcpy(database[4][0], "FRRING");
strcpy(database[4][1], "CRASH");
strcpy(database[4][2], "FOO");
//(gdb) ptype record_compare_field_1
//type = int (char (*)[8], …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 对结构数组进行排序qsort,但它没有正确对内容进行排序。结构节点由起始顶点、结束顶点以及从顶点“a”到达顶点“b”的成本组成。
我正在编写克鲁斯卡尔算法的代码
#include <stdio.h>
#include <stdlib.h>
int v, e;
typedef struct node {
int a;
int b;
int cost;
} node;
int compare(const void *a, const void *b) {
const node *x = *(node **)a;
const node *y = *(node **)b;
return (x->cost > y->cost) ? 1 : 0;
}
int main() {
scanf("%d %d", &v, &e);
int i;
node *arr[e];
for (i = 0; i < e; i++) {
int a, b, cost;
scanf("%d %d %d", …Run Code Online (Sandbox Code Playgroud) 我正在尝试学习 C,并遇到了一个用于qsort对字符串数组进行排序的示例。
我正在努力理解以下内容:
int CompareWords(const void *a, const void *b) {
char **str1 = (char **)(a);
char **str2 = (char **)(b);
char *c1 = (char *)a;
char *c2 = (char *)b;
return strcmp(c1, c2);
// return strcmp(*str1, *str2);
}
Run Code Online (Sandbox Code Playgroud)