您的位置:首页 > 编程语言 > C语言/C++

c++实用语法

2016-02-16 21:21 344 查看
数组的快捷初始化

int inq[110]

memset(inq, 0, sizeof(inq));

string到char数组的转换:

string str ("Please split this sentence into tokens");
char * cstr = new char [str.length()+1];
strcpy (cstr, str.c_str());

快速排序qsort函数

需要包含<stdlib.h> qsort   

功 能: 使用快速排序例程进行排序   

用 法: void qsort(void *base, int nelem, int width, int (*fcmp)(const void *,const void *));   

各参数

1 待排序数组首地址

2 数组中待排序元素数量

3 各元素的占用空间大小

4 指向函数的指针

一、对int类型数组排序

int cmp ( const void *a , const void *b ) { return *(int *)a - *(int *)b; } qsort(num,100,sizeof(num[0]),cmp);

二、对char类型数组排序(同int类型)

int cmp( const void *a , const void *b ) { return *(char *)a - *(int *)b; } qsort(word,100,sizeof(word[0]),cmp);

三、对double类型数组排序(特别要注意)

int cmp( const void *a , const void *b ) { return *(double *)a > *(double *)b ? 1 : -1; } qsort(in,100,sizeof(in[0]),cmp);

四、字符串数组

int compare_c(const void *a,const void *b) {return strcmp((char*)a,(char*)b);} qsort(f,n,16,compare_c);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: