这是一个简单的 C++ 测试代码,它只会按 StudentId(整数)对学生结构进行排序:
#include <iostream>
using namespace std;
struct student {
int grade;
int studentId;
string name;
};
int studentIdCompareFunc(const void * student1, const void * student2);
int main()
{
const int ARRAY_SIZE = 10;
student studentArray[ARRAY_SIZE] = {
{81, 10009, "Aretha"},
{70, 10008, "Candy"},
{68, 10010, "Veronica"},
{78, 10004, "Sasha"},
{75, 10007, "Leslie"},
{100, 10003, "Alistair"},
{98, 10006, "Belinda"},
{84, 10005, "Erin"},
{28, 10002, "Tom"},
{87, 10001, "Fred"},
};
qsort(studentArray, ARRAY_SIZE, sizeof(student), studentIdCompareFunc);
for (int i = 0; …Run Code Online (Sandbox Code Playgroud) 我想long long在C中按四百万s 的顺序排序.通常我只是malloc()一个缓冲区用作数组并调用qsort()但是四百万*8字节是一大块连续内存.
最简单的方法是什么?为此,我对速度感到轻松.我不想使用任何库,结果将需要在Windows和Linux下的适度上网本上运行.
我正在尝试对已创建的单链表进行排序,并将其所有项目,指针初始化.我正在尝试使用qsort()C库函数,如下所示.它似乎没有排序列表.它给了我编译器错误说: 'item'的左边指定代码中下面显示的未定义的struct/union'LINKED_LIST_S'.
struct LINKED_LIST_S
{
int item;
struct LINKED_LIST_S * next;
} ;
typedef int (*cmpfn)(const void *ptr1, const void *ptr2);
int mylistsort(my_list_t list, cmpfn f1)
{
qsort ( list , 100, sizeof (struct LINKED_LIST_S), (fn) );
return -1;
}
int sort_fn_ascend(const void *ptr1, const void *ptr2)
{
int a = (*(LINKED_LIST_S *)ptr1).item; //Compiler error
int b = (*(LINKED_LIST_S *)ptr2).item; //Compiler error
return b - a;
}
int sort_fn_descend(const void *ptr1, const void *ptr2)
{
int a …Run Code Online (Sandbox Code Playgroud) 我指的是一个比较qsort vs stdsort性能的上一个链接.
我写了一个C程序,填充了一个大的std::map,我想对数组进行排序qsort.我正在使用.
typedef std::map<uint16_t, uint32_t> TSrcMap;
TPSrcMap sp;
TSrcMap::iterator its;
/*Code to populate the array_start.*/
/*Code to populate the array_end.*/
typedef struct port_count
{
uint32_t port_number;
uint32_t port_count;
}port_count_t;
port_count_t pcount[10];
memset(pcount,0,sizeof(pcount));
size_t structs_len = sizeof(pcount)/sizeof(port_count_t);
for(its = stcp.begin(); its != stcp.end();its++)
{
if(pcount[smallest_index].port_count < (*its).second)
{
pcount[smallest_index].port_count = (*its).second;
pcount[smallest_index].port_number = (*its).first;
/*qsort(pcount, structs_len, sizeof(port_count_t), struct_cmp_by_port_count);*/
std::sort(pcount,sizeof(port_count_t));
}
}
Run Code Online (Sandbox Code Playgroud)
该qsort函数正确排序数组.我想比较qsortwith 的性能,std::sort但调用std::sort调用给出了编译错误
没有匹配的呼叫功能
‘sort(port_count_t [10], …
我尝试使用qsort对一个double值数组进行排序,但它似乎不起作用.想知道这里出了什么问题?
#include <stdio.h>
#include <stdlib.h>
static double compare (const void * a, const void * b)
{
if (*(double*)a > *(double*)b) return 1;
else if (*(double*)a < *(double*)b) return -1;
else return 0;
}
int main() {
int idx;
double* sum_least_square_err;
sum_least_square_err = (double*) malloc (2500*2500*sizeof(double));
sum_least_square_err[0] = 0.642;
sum_least_square_err[1] = 0.236;
sum_least_square_err[2] = 0.946;
idx = 3;
qsort(sum_least_square_err, idx, sizeof(sum_least_square_err), compare);
int i;
for (i=0; i<idx; i++){
fprintf(stderr,"sum_least_square_err[%d] = %.3f\n", i, sum_least_square_err[i]);
}
fprintf(stderr,"MAEE = %.3f\n", sum_least_square_err[idx/2]);
free(sum_least_square_err);
} …Run Code Online (Sandbox Code Playgroud) 以下是我的代码:
我似乎无法有效地使用qsort ...它在填充了名称和开始时间之后将我的数组转换为0 ...这是我的qsort调用的问题吗?或者qsort本身.
带结构的标题如下:
/**
* Simulation of a process scheduler
*/
//#ifndef SCHEDULER_H_
#define SCHEDULER_H_
#include <stddef.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
/* types */
/** units of time */
typedef long time;
/** process identifier */
typedef int pid;
/** Information about a job of interest to the task scheduler */
struct job_data {
/* pid of this process */
pid pid;
/* time process starts */
time …Run Code Online (Sandbox Code Playgroud) 我试图通过变量a对结构节点进行排序,但结果证明是错误的.
我的结果:
{5, 4}, {6, 2}, {7, 3}, {4, 1}, {3, 7}, {1, 3}, {0, 0},
Run Code Online (Sandbox Code Playgroud)
我的代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int x;
int y;
} n;
int num = 7;
int compare(const void *ele1, const void *ele2) {
n *px, *py;
px = (n *) ele1;
py = (n *) ele2;
return px->x < py->x;
}
int main() {
n node[7] = {
{4, 1},
{6, 2},
{1, 3},
{5, 4},
{7, 3},
{3, 7} …Run Code Online (Sandbox Code Playgroud) 我有一个结构,我需要按升序排序:
typedef struct CallLogSearchDataStruct
{
char * date;
char * time;
char * bParty;
char * aParty;
float duration;
char * cleardownCause;
struct CallLogSearchOutboundStruct * outboundLegs;
int maxDataCol;
} callLogSearchDataStruct;
Run Code Online (Sandbox Code Playgroud)
我需要根据日期和时间按升序对结构进行排序.日期和时间采用以下格式
日期:16/05/2011时间:01:20:03
我需要按升序对上面的两个字段进行排序,我一直在看qsort,但我无法找到一种能够做到这一点的方法.我按以下方式调用该函数.
qsort(callLogSearchData, dataRow, sizeof(callLogSearchDataStruct), sortCompare);
Run Code Online (Sandbox Code Playgroud)
我的功能如下
int sortCompare(const void * a, const void * b)
{
const callLogSearchDataStruct *u1 = a;
const callLogSearchDataStruct *u2 = b;
if (u1->date < u2->date)
{
return -1;
}
else if (u1->date > u2->date)
{
return 1;
}
else
{ …Run Code Online (Sandbox Code Playgroud) 我有一个结构:
typedef struct personalData
{
char name[20];
char * remarks;
int age;
float weight;
} personalData;
Run Code Online (Sandbox Code Playgroud)
我需要按重量对数据进行排序qsort().这是我的weightSort功能:
void weightSort(personalData * data[], int len)
{
qsort(data, len, sizeof(struct personalData *), structSortWeight);
}
Run Code Online (Sandbox Code Playgroud)
where len= 10(在使用某些不同的函数之前计算,但可能无关紧要),data[]定义main()如下:
struct personalData * data[10];
Run Code Online (Sandbox Code Playgroud)
最后structSortWeight:
int structSortWeight(const void *a, const void *b)
{
personalData *p1 = (personalData *)a;
personalData *p2 = (personalData *)b;
return (p1->weight - p2->weight);
}
Run Code Online (Sandbox Code Playgroud)
我的程序在开始排序时崩溃了.我想补充一点,当我改变了第三个参数中qsort(),以sizeof(float)它不会崩溃,但p1->weight并 …
我有一个结构subPolygon和一个指向该结构的指针向量.我试图qsort在矢量上使用,但比较器功能显示为不兼容.我究竟做错了什么?
int cmpFunc(const void *p, const void *q) {
struct subPolygon* p1 = *((struct subPolygon**)p);
struct subPolygon* p2 = *((struct subPolygon**)q);
int s1 = p1->size;
int s2 = p2->size;
if (s1-s2 < 0 ) return -1;
if (s1 == s2) return 0;
return 1;
}
Run Code Online (Sandbox Code Playgroud)
这是向量的声明:
vector<subPolygon*> subPolygons;
Run Code Online (Sandbox Code Playgroud)
该qsort呼叫:
qsort(&subPolygons[0], subPolygons.size(), sizeof(struct subPolygon*),cmpFunc);
Run Code Online (Sandbox Code Playgroud)
编辑:似乎问题是一个额外的错误:
cmpFunc: non-standard synatx; use & to create pointer to a member "
我的比较器函数是一个类的成员.Qsort是从该类的成员调用的.使我的比较器功能静态解决了这个问题.
qsort ×10
c ×7
c++ ×3
sorting ×2
arrays ×1
dictionary ×1
large-data ×1
linked-list ×1
std ×1
stdmap ×1
struct ×1
text ×1