#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) 所以,我有一个比较两个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) 我正在对自定义对象进行排序,作为比较器,我有一个随机打破平局的函数(这很重要,因为我多次调用它,并且每次都想要不同的顺序)。这意味着我不能使用 std::sort,因为这个比较器不满足严格弱排序的要求(因为它不保持传递性)。
这可以用 qsort 来完成,但我还有另一个问题:我需要将随机引擎(例如 m19937)传递给比较器函数。由于其他原因,我无法使用全局随机引擎(它由多个线程调用,并且具有全局变量会使程序具有不确定性)。将参数传递给比较器函数可以解决我的问题(我将传递随机引擎) - 但我不知道如何将参数传递给比较器。这可能吗 ?
据我所知, qsort 不能将函子作为比较器。
我正在努力整合代码库(将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) 我有一些旧的代码,用于qsort对MFC CArray的结构进行排序,但我看到偶尔的崩溃可能会导致多个线程同时调用qsort.我使用的代码看起来像这样:
struct Foo
{
CString str;
time_t t;
Foo(LPCTSTR lpsz, time_t ti) : str(lpsz), t(ti)
{
}
};
class Sorter()
{
public:
static void DoSort();
static int __cdecl SortProc(const void* elem1, const void* elem2);
};
...
void Sorter::DoSort()
{
CArray<Foo*, Foo*> data;
for (int i = 0; i < 100; i++)
{
Foo* foo = new Foo("some string", 12345678);
data.Add(foo);
}
qsort(data.GetData(), data.GetCount(), sizeof(Foo*), SortProc);
...
}
int __cdecl SortProc(const void* elem1, …Run Code Online (Sandbox Code Playgroud) 我使用内置的qsort来排序结构数组.但是在调用qsort之后,数组中的最后一个元素似乎已经具有了我的值,我将其设置为空.
这是我的代码......
int numEntries = 5;
TvEntry* entries[numEntries]; //create array
//Entries get added to the array here...
qsort( *entries, numEntries, sizeof(TvEntry*), &compareByName ); //sort
displayAll( entries, numEntries ); //display
//here is my sort method
int compareByName( const void* val1, const void* val2 )
{
const TvEntry* entry1 = (TvEntry*)val1;
const TvEntry* entry2 = (TvEntry*)val2;
return strcasecmp( entry1->title, entry2->title );
}
//here is my display method
void displayAll( TvEntry* entries[], int length )
{
if( entries == NULL )
{
printf( …Run Code Online (Sandbox Code Playgroud) 我正在尝试对指向结构的指针数组进行排序,其中要比较的键是结构的属性之一.
我想这可能是比较方法.
这是一个示例代码.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct BINARY_ARRAY_RECORD {
char *name;
};
int compare(const void *node1, const void *node2) {
return strcmp(
((struct BINARY_ARRAY_RECORD *) node1)->name,
((struct BINARY_ARRAY_RECORD *) node2)->name
);
}
int main()
{
struct BINARY_ARRAY_RECORD **records;
records = malloc(sizeof(struct BINARY_ARRAY_RECORD *) * 2);
records[0] = malloc(sizeof(struct BINARY_ARRAY_RECORD));
records[1] = malloc(sizeof(struct BINARY_ARRAY_RECORD));
records[0]->name = malloc(sizeof(char) * (strlen("string2") + 1));
records[1]->name = malloc(sizeof(char) * (strlen("string1") + 1));
strcpy(records[0]->name, "string2");
strcpy(records[1]->name, "string1");
qsort(records, 2, sizeof(records[0]), compare);
printf("%s\n", records[0]->name); …Run Code Online (Sandbox Code Playgroud)