最近由于学习Linux C程序设计等内容再次遇到头疼的指针,所以特此重新学习下指针和C++的面向对象编程相关的知识,发现以前不会的现在理解起来很easy。
qsort函数原型(C语言标准库函数)
功 能: 使用快速排序例程进行排序
用 法: void qsort(void *base, int nelem, int width, int (*fcmp)(const void *,const void *));
参数:1 待排序数组首地址 2 数组中待排序元素数量 3 各元素的占用空间大小 4 指向函数的指针,用于确定排序的顺序
这里就用到了指针,void*个人认为可以理解成Java里的Object, int (*fcmp)(const void *,const void *):函数指针,const就是说明后面的参数不可以改变,增加程序的健壮性。
排序就需要用到交换,即令人头疼的swap函数
1、最简单的实现
void swap(int a,int b){
int temp;
temp = a;a = b; b = temp;
}
此种方法在各种C/C++教科书上都说不能实现a和b的交换,这里就不解释了
2、指针形参
void swap(int&,int&);
void swap(int *a,int *b){
int temp;
temp = *a;*a = *b; *b = temp;
}
这个肯定能实现交换,&直接取址,*间接取址
3、引用形参
void swap(int&,int&);
void swap(int &a,int &b){
int temp;
temp = a;a = b; b = temp;
}
4、异或运算
a ^= b;
b ^= a;
a ^= b;
//a^b^a = b
5、qsort源码用到的
void swap(char *a,char* b,int width){
char temp;
while(width --){
temp = *a,*a++ = *b,*b++ = temp;
}
}
width:待交换元素的占用空间大小,此种方法和第三种方法是同样的原理
QSORT源码:
#include
#include
using namespace std;
struct Node{
char *word;
int cnt;
};
void qsort(void *base,int nleft, int nright,int(*fcmp)(void*,void*));
int cmp(void* a,void* b){//(int*)属强制转换,再*间接取址
return *(int *)a - *(int *)b;
}
int cmp2(void* a,void* b){
Node pa = *(Node *)a;
Node pb = *(Node *)b;
return strcmp(pa.word,pb.word);
}
void swap(char *a,char* b,int width){
char temp;
while(width --){
temp = *a,*a++ = *b,*b++ = temp;
}
}
void qsort(void* base,int nleft,int nright,int width,int (*fcmp)(void*,void*)){
if(nleft >= nright) return;
char *lo = (char*)base + width*nleft;
char *hi = (char*)base + width*((nleft + nright)/2);
swap(lo,hi,width);
int x = nleft;
lo = (char*)base + width * nleft;//standard
//cout<<"St: "<<(*(Node*)lo).word<
深入分析qsort库函数(一)http://www.programfan.com/blog/article.asp?id=14298
深入分析qsort库函数(二)http://blog.pfan.cn/accelerator/14363.html
深入分析qsort库函数(三)http://blog.pfan.cn/accelerator/14463.html
有人说C语言很简单,所有IT人才都会,所以学习在C环境下做工程项目没用,我想问问大家的意见。