我有一个结构:
struct pkt_
{
double x;
double y;
double alfa;
double r_kw;
};
typedef struct pkt_ pkt;
Run Code Online (Sandbox Code Playgroud)
这些结构的表格:
pkt *tab_pkt;
tab_pkt = malloc(ilosc_pkt * sizeof(pkt));
Run Code Online (Sandbox Code Playgroud)
我想要做的是排序tab_pkt的tab_pkt.alfa和tab_pkt.r:
qsort(tab_pkt, ilosc_pkt, sizeof(pkt), porownaj);
Run Code Online (Sandbox Code Playgroud)
porownaj是比较函数,但如何写呢?这是我的"草图":
int porownaj(const void *pkt_a, const void *pkt_b)
{
if (pkt_a.alfa > pkt_b.alfa && pkt_a.r_kw > pkt_b.r_kw) return 1;
if (pkt_a.alfa == pkt_b.alfa && pkt_a.r_kw == pkt_b.r_kw) return 0;
if (pkt_a.alfa < pkt_b.alfa && pkt_a.r_kw < pkt_b.r_kw) return -1;
}
Run Code Online (Sandbox Code Playgroud) 编程珍珠只是我或者这个代码是错误的(quicksort想要2个const空洞,不是吗?)如果是这样,我的解决方案是对的吗?道歉,只是学习......
int wordncmp(char *p, char* q)
{ int n = k;
for ( ; *p == *q; p++, q++)
if (*p == 0 && --n == 0)
return 0;
return *p - *q;
}
int sortcmp(char **p, char **q)
{ return wordncmp(*p, *q);
}
...
qsort(word, nword, sizeof(word[0]), sortcmp);
Run Code Online (Sandbox Code Playgroud)
这是一个解决方案吗?
int sortcmp(const void *p, const void *q)
{ return wordncmp(* (char * const *) p, * (char * const *) q);
}
Run Code Online (Sandbox Code Playgroud) 我正在使用内置函数qsort()来对class item指针向量进行排序.
class item {
int value;
vector<char> c;
...
...
};
//Declaration of vector
vector<item*> items;
//Function Call
qsort(&items, items.size(), sizeof(item*), value_sort);
int value_sort(const void* a, const void* b)
{
item* pa = *(item**) a;
item* pb = *(item**) b;
if (pb->value < pa->value)
return 1;
else if (pa->value < pb->value)
return -1;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在调试器模式下,指针既不指向pa也不pb指向有效位置.由class items指向的所有数据成员集合pa或pb包含垃圾值.我哪里弄错了?我也不确定双指针的用法.
谢谢.
我的任务是用户输入包含月份,日期和年份的n行,格式为"1月12日99".
我必须按时间顺序排序日期列表,首先使用qsort逐年,然后按天,然后按月.
我的问题是我不知道如何对多个索引进行qsort.我已经完成了今年的工作,但是在那之后我不知道如何在当天进行调整,因为它肯定会在白天对它进行排序,但这些年将再次混乱?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef int (*compfn)(const void*, const void*);
struct date
{
char month[9]; //Maximum of 9 characters in a month
int day; //The day of the month (e.g. 18)
int year; //The year of the date
};
int sortDates(struct date *elem1, struct date *elem2)
{
if (elem1 -> year < elem2 -> year)
{
return -1;
}
else
if (elem1->year > elem2->year)
{
return 1;
}
else
return 0;
}
main()
{
int …Run Code Online (Sandbox Code Playgroud) 从引用的各种来源我知道内置的C函数,stable_sort是稳定的但qsort是不稳定的.如果是这种情况,我们为什么要使用qsort呢?这不是多余的吗?为什么不使用stable_sort呢?
我一直在寻找qsort的不同实现,并且在这里找到了一行(https://code.woboq.org/userspace/glibc/stdlib/qsort.c.html),我不明白.它看起来像一个函数指针声明.我很感激任何帮助.我已经根据需要包含了尽可能多的代码(注意到这一行),我想回答这个问题.如果没有,请告诉我,谢谢.
typedef struct
{
char *lo;
char *hi;
} stack_node;
void _quicksort (void *const pbase, size_t total_elems, size_t size, cmp_t cmp, void *arg)
{
char *base_ptr = (char *) pbase;
const size_t max_thresh = 4 * size;
if (total_elems == 0)
return;
if (total_elems > 4)
{
char *lo = base_ptr;
char *hi = &lo[size * (total_elems - 1)];
stack_node stack[(8 * sizeof(size_t))];
stack_node *top = stack;
/* Line below is a function pointer declaration? Initializes struct? */ …Run Code Online (Sandbox Code Playgroud) 我非常喜欢qsortC中的函数.它非常易于使用,并且允许我拖延学习C++模板类型.我有几个问题:
我试着学习c-library的qsort功能stdlib.这甚至提供c++.但我不明白如何使用它们来排序c++字符串.我不确定sizeof()操作员的参数应该是什么,以及我的compare_str代码是否正确.我试过这段代码:
#include<iostream>
#include<cstdlib>
using namespace std;
#include<string>
int compare_str( const void *a, const void *b){
string obj = (const char*)a;
string obj1 = (const char*)b;
return obj.compare(obj1);
}
int main(){
string obj[4] = {"fine", "ppoq", "tri", "get"};
qsort(obj, 4, sizeof(obj[0].length()), compare_str);
for( int i=0; i<4; i++)
cout<<obj[i]<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的输出是:
ppoq
tri
get
fine
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚错误.请帮忙.
我可以对单词指针数组进行排序,使它们按字母顺序排序,问题是我需要对整数数组(特定单词的使用次数)进行排序,以便整数与它们的位置相同各自的话:
我的代码:
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) 这是代码,它通过avg运行对板球运动员的数据进行排序.该qsort函数显示错误:
[错误] C:\用户\编码器\文件\ C-免\ TEMP\Untitled3.cpp:29:错误:从无效转变
int (*)(cricketer*, cricketer*)至int (*)(const void*, const void*)[错误] C:\ Users\Encoder\Documents\C-Free\Temp\Untitled3.cpp:29:错误:初始化`void qsort的参数4(void*,size_t,size_t,int()(const void,const void)*))"
#include<stdlib.h>
struct cricketer //structure for details of cricketer
{
int avg_run;
char name[20];
int age;
int match_no;
} c[4];
int sort(struct cricketer *a, struct cricketer *b); //pre-defining sort function
int main() //main function
{
int i, s;
for (i = 0; i < 3; i++) //enumerating structure records.
{
printf("enter the name of cricketer "); …Run Code Online (Sandbox Code Playgroud)