C 中标准库函数 qsort 的用法
2014-02-05 17:31
344 查看
一、《C语言函数库》对qsort的说明(p110)
qsort:快速排序函数
函数原型:void qsort(void *base, int nelem, int width, int(*fcmp)(const void *, const *))
头文件 :#include<stdlib.h>
是否是标准函数:是
函数功能:对记录 进行从小到大的快速排序。参数base指向存放待排序列的数组的首地址,nelem为数组中元素的个数,width为每个元素的字节数,int(*fcmp)(const void *, const *)为由用户提供的比较函数。
返回值:无
二、简单例子
三、特别注意
1.qsort第四个参数,即上例中的函数cmp,返回值必须是int,两个参数的类型必须都是const void *类型,因此在写比较复杂的比较函数时,可提前先进行强制类型转换,转换成相应的类型;
2.对于书中提供的例子编译通不过,也有另一种解决方法,即在调用时直接进行强制类型转换,即将上述两处分别更改为
int CMP(int * a ,int * b){//特别注意1
qsort(sort,10,sizeof(int), (int (*)(const void *, const void *)) CMP);//特别注意2
一般使用第一种方法。
四、其他类型的比较函数的书写
1.字符串的比较,直接使用 strcmp函数;
2.结构体的比较
见http://blog.csdn.net/lonely_fish/article/details/18940561
此外补充,结构体的用法:
struct是结构体的关键字,用来声明结构体变量如
struct student
{ char num[10];
char name[20];
int age;
};
struct student stu[10]来声明一个结构体数组
typedef是用来定义新的类型名来代替已有的类型名,
可将上面的结构体定义为
typedef struct student
{ char num[10];
char name[20];
int age;
}student;
也就是说,将原来的struct student 重新定义为 stud;
可以直接用 student stu[10]来声明一个结构体数组
qsort:快速排序函数
函数原型:void qsort(void *base, int nelem, int width, int(*fcmp)(const void *, const *))
头文件 :#include<stdlib.h>
是否是标准函数:是
函数功能:对记录 进行从小到大的快速排序。参数base指向存放待排序列的数组的首地址,nelem为数组中元素的个数,width为每个元素的字节数,int(*fcmp)(const void *, const *)为由用户提供的比较函数。
返回值:无
二、简单例子
/* C中qsort函数的使用 */ #include<stdio.h> #include<stdlib.h> int CMP(const void * a ,const void * b){//特别注意1 return *(int *)a-*(int *)b; } int main() { int sort[10]={3,2,6,12,1,7,-5,9,30,16}; int i; printf("\nThe array that is before sort \n"); for(i=0;i<10;i++){ printf("%d ",sort[i]); } qsort(sort,10,sizeof(int),CMP);//特别注意2 printf("\nThe array that is after sort\n"); for(i=0;i<10;i++){ printf("%d ",sort[i]); } printf("\n"); return 0; }
三、特别注意
1.qsort第四个参数,即上例中的函数cmp,返回值必须是int,两个参数的类型必须都是const void *类型,因此在写比较复杂的比较函数时,可提前先进行强制类型转换,转换成相应的类型;
2.对于书中提供的例子编译通不过,也有另一种解决方法,即在调用时直接进行强制类型转换,即将上述两处分别更改为
int CMP(int * a ,int * b){//特别注意1
qsort(sort,10,sizeof(int), (int (*)(const void *, const void *)) CMP);//特别注意2
一般使用第一种方法。
四、其他类型的比较函数的书写
1.字符串的比较,直接使用 strcmp函数;
/* C中qsort函数的使用 */ //字符串数组的比较 #include<stdio.h> #include<stdlib.h> #include<string.h> int CMP(const void* a ,const void * b){//特别注意1 return strcmp((char*)a,(char *)b); } int main() { char list[5][4]={"cat","car","cab","cap","can"}; int i; qsort(list,5,sizeof(list[0]), CMP);//特别注意2 for(i=0;i<5;i++){ printf("%s\n",list[i]); } return 0; }
2.结构体的比较
见http://blog.csdn.net/lonely_fish/article/details/18940561
此外补充,结构体的用法:
struct是结构体的关键字,用来声明结构体变量如
struct student
{ char num[10];
char name[20];
int age;
};
struct student stu[10]来声明一个结构体数组
typedef是用来定义新的类型名来代替已有的类型名,
可将上面的结构体定义为
typedef struct student
{ char num[10];
char name[20];
int age;
}student;
也就是说,将原来的struct student 重新定义为 stud;
可以直接用 student stu[10]来声明一个结构体数组
相关文章推荐
- SOS
- DIY远程控制开关(tiny6410+LED+yeelink+curl)
- 关于编码的一切
- 1061成绩排序(c中qsort,strcmp函数的使用)
- HDU 2138 How many prime numbers
- 处理散列冲突的方法 - 数据结构和算法85
- 紫外线消毒器水处理杀菌的四大优势分析
- JQuery Introduction
- shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such fi
- C#.NET 大型通用信息化系统集成快速开发平台 4.1 版本 - 对外不要提供Delete方法加强软件的安全性
- [floyd]HDU 1181变形课
- C++回顾之引用
- 孙鑫 VC++深入详解第15课——多线程
- Struts2模型驱动
- SPOJ 345 Mixtures (区间DP&前缀数组)
- 如果系统要使用超大整数(超过long长度范围),请你设计一个数据结构来存储这种超大型数字以及设计一种算法来实现超大整数加法运算)
- Nodejs Native AddOn的编写
- 【C#小知识】C#中一些易混淆概念总结(四)---------解析Console.WriteLine()
- JavaSE学习笔记
- java学习之面试题4