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);
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);
相关文章推荐
- 好用的C++数据库访问层
- 20160203.CCPP体系详解(0013天)
- C语言程序设计-第7章 用函数实现模块化程序设计
- C++运算符优先级列表
- 198. House Robber(C++实现)
- 20160202.CCPP体系详解(0012天)
- 【POJ 3461】Oulipo 中文题意&题解&代码(C++)
- 【C++】顺时针打印矩阵
- 20160201.CCPP体系详解(0011天)
- 20160131.CCPP体系详解(0010天)
- bzoj 3238 差异
- 20160130.CCPP体系详解(0009天)
- 二叉树层次遍历(C语言实现)
- 面向对象课程 - 寒假第三次作业 - C++计算器项目初始部分
- c++參数传递
- C语言-内存对齐
- C++STL-全排列
- C++中指针和引用的区别
- C语言语法
- C语言:IF-ELSE的配对问题