使用标准c库函数对struct(ure)s数组进行排序? [英] Sort an array of struct(ure)s using standard c library functions?

查看:86
本文介绍了使用标准c库函数对struct(ure)s数组进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个结构数组,每个都是这种类型



struct A {

char filename [];

off_t filesize;

};



我正在尝试根据filesize对这个结构数组进行排序,以便我可以打印这个数据数据中最大的5个文件(包括文件名及其大小)。





我也试图避免手动写泡泡排序或任何算法。那么我可以获取标准c库中的任何函数吗?

I have an array of structures, each of this type

struct A {
char filename[];
off_t filesize;
};

I''m trying to sort this array of structs according to filesize, so that I can , for instance, print the 5 largest file (include filename and its size) from this array of data.


Also I''m trying to avoid manually write up bubble sort or whatever algorithm. So are there any functions from standard c libraries that I can grab?

推荐答案

C标准库中有一个名为qsort的函数可以执行您想要的操作。你现在要做的就是查看文档。



干杯!
There is a function in the C standard library called qsort that will do what you want to. All you have to do now is to look into the documentation.

Cheers!


这篇关于使用标准c库函数对struct(ure)s数组进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆