stdlib
system(pause);
- int cmp(const void *a ,const void *b)
- {
- return *(int *)a - *(int *)b ; //从小到大排序,把a,b位置反过来就是从大到小
- }
qsort:void qsort(void *base,int nelem,int width,int (*fcmp)(const void *,const void *));base:数组 nelem查找长度 一般是数组长度 width每个元素所占内存 一般用sizeof();
比如对一个s[n]的数组排列其从s[i]开始的m个元素,只需要在第一个和第二个参数上进行一些修改:
void qsort(&s[i],m,sizeof(s[i]),cmp);
二、对char类型数组排序(同int类型) //注意要用strlen判断终止 不要排序\0会出错 char word[100]; Sample: int cmp( const void *a , const void *b )
{
return *(char *)a - *(char *)b;
} qsort(word,100,sizeof(word[0]),cmp); 三、对double类型数组排序(特别要注意) double in[100]; int cmp( const void *a , const void *b )
{
return *(double *)a > *(double *)b ? 1 : -1;
} qsort(in,100,sizeof(in[0]),cmp); 四、对结构体一级排序 struct In
{
double data;
int other;
}s[100] //按照data的值从小到大将结构体排序,关于结构体内的排序关键数据data的类型可以很多种,参考上面的例子写 int cmp( const void *a ,const void *b)
{
return (*(In *)a).data > (*(In *)b).data ? 1 : -1;
} qsort(s,100,sizeof(s[0]),cmp); 五、对结构体二级排序 struct In
{
int x;
int y;
}s[100]; //按照x从小到大排序,当x相等时按照y从大到小排序 int cmp( const void *a , const void *b )
{
struct In *c = (In *)a;
struct In *d = (In *)b;
if(c->x != d->x) return c->x - d->x;
else return d->y - c->y;
} qsort(s,100,sizeof(s[0]),cmp); 六、对字符串进行排序 struct In
{
int data;
char str[100];
}s[100]; //按照结构体中字符串str的字典顺序排序 int cmp ( const void *a , const void *b )
{
return strcmp( (*(In *)a).str , (*(In *)b).str );
} qsort(s,100,sizeof(s[0]),cmp); 七、计算几何中求凸包的cmp int cmp(const void *a,const void *b) //重点cmp函数,把除了1点外的所有点,旋转角度排序
{
struct point *c=(point *)a;
struct point *d=(point *)b;
if( calc(*c,*d,p[1]) < 0) return 1;
else if( !calc(*c,*d,p[1]) && dis(c->x,c->y,p[1].x,p[1].y) < dis(d->x,d->y,p[1].x,p[1].y)) //如果在一条直线上,则把远的放在前面
return 1;
else return -1;
} bsearch:二分法搜索 需要注意的是,
数据必须是经过预先排序的,而排序的规则要和comp所指向比较子函数的规则相同。
如果查找成功则返回数组中匹配元素的地址,反之则返回空。
对于有多于一个的元素匹配成功的情况,bsearch()未定义返回哪一个。
用法:pItem = (int*) bsearch (&key(要查找的元素), values(数组名), 6(查找长度), sizeof (int)(内存宽度), compareints);
/* bsearch example with strings */
#include <stdio.h>
#include <stdlib.h>
char strvalues[][20] = {"some","example","strings","here"}; //二维字符数组 相当于存放n个一维字符数组
int cmp(const void * a, const void * b)
{
return ( *(char*)a - *(char*)b );
}
int main ()
{
char * pItem;
char key[20] = "example"; //要查找的元
/* sort elements in array: */
qsort (strvalues, 4, 20, cmp);
/* search for the key: */字符串
pItem = (char*) bsearch (key, strvalues, 4, 20, cmp);
if (pItem!=NULL)
printf ("%s is in the array.\n",pItem);
else
printf ("%s is not in the array.\n",key);
return 0;
}
stdlib的更多相关文章
- stdlib.h stdio.h
stdlib.h 即standard library标准库头文件.stdlib.h里面定义了五种类型.一些宏和通用工具函数. 类型例如size_t.wchar_t.div_t.ldiv_t和lldiv ...
- ansible操作远程服务器报Error: ansible requires the stdlib json or simplejson module, neither was found!
通过ansible执行远程命令时报如下异常: Error: ansible requires the stdlib json or simplejson module, neither was fou ...
- System.TypeInitializationException: The type initializer for 'Mono.Unix.Native.Stdlib' threw an exception.
08-31 17:02:03: ### DEBUG ##########################System.TypeInitializationException: The type ini ...
- stdlib 头文件
stdlib 头文件即standard library标准库头文件.stdlib.h里面定义了五种类型.一些宏和通用工具函数. 类型例如size_t.wchar_t.div_t.ldiv_t和lldi ...
- stdlib.h 头文件
stdlib 头文件即standard library标准库头文件.stdlib.h里面定义了五种类型.一些宏和通用工具函数. 类型例如size_t.wchar_t.div_t.ldiv_t和lldi ...
- c语言实用功能库函数#include<stdlib.h>
实用函数<stdlib.h> 在头文件<stdlib.h>中说明了用于数值转换.内存分配以及具有其他相似任务的函数. 1 atof #include <stdlib.h& ...
- #include <stdlib.h>
1 _itoa 2 atoi 3 rand() 4 srand 1 _itoa _itoa(int value,char*string,int radix); int value 被转换的整数,cha ...
- mac 下 clang++ 找不到头文件 stdlib.h
因为要用 openmp库,用 clang++ 编译 c++程序,出现了如下报错: clang++ xx.cpp -o xx -fopenmp /usr/local/Cellar/llvm/7.0.0/ ...
- C语言中你可能不熟悉的头文件(stdlib.h)
C语言中你可能不熟悉的头文件<cstdlib>(stdlib.h) C Standard General Utilities Library (header) C标准通用工具库(头文件) ...
随机推荐
- ES questions
Be Careful of Cardinality Numeric and date fields are indexed in such a way that ranges are efficien ...
- Unity代码热更新方案 JSBinding + SharpKit 首页
目前Unity的代码更新方案有很多,主要以lua为主. JSBinding + SharpKit 是一种新的技术,他做了两件事情: JSBinding将C#导出到 JavaScript (引擎是 Mo ...
- excel让每个单元格的宽度随着字体自动变动的两种方式(有更好方法的大神,请忽略,求评论下)
1.打开在EXCEL的工作表,点击工作表左上方的方框,选中整个工作表,将鼠标一致A列的右边线处,也就是A列和B列中间的分界线处,光标会变成十字,鼠标左键双击,列宽会调整到最恰当处.截图如下
- ABBYY可以给我们解决那些问题
不同的行业组织和企业有不同的业务流程和规定,在OCR文字识别领域,ORC文字识别软件ABBYY给各个行业都提供了有效解决方案,满足其特定需求的同时还帮助他们提高业务流程处理效率,降低成本,全球大量的纸 ...
- ORA-12170: TNS: 连接超时
ORA-12170: TNS: 连接超时 如果在本机可以正常使用,可是到局域网中的其他机器就出现“ORA-12170:TNS:连接超时 解决方法: 1.cmd-----ping ip地址 查看网络问题 ...
- 09-Java 工程结构管理
(一)Java 工程结构管理 1.什么是Build Path: -- 一般包括:JRE运行时库 第三方功能扩展库(*.jar 格式文件) 其他的工程 其他的源代码或Class 文件 为什么使用~ :通 ...
- shell之函数
function 所有函数在使用前必须定义.这意味着必须将函数放在脚本开始部分,直至shell解释器首次发现它时,才可以使用.调用函数仅使用其函数名即可.可以将函数看作是脚本中的一段代码,但是有一个主 ...
- maxsdk sample中3dsexp.rc点不开并提示specstrings.h中找不到sal.h解法
在网上下载sal.h文件并拷贝到specstrings.h所在目录(C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Include)即可. sa ...
- Linux-Hostname-details
转自:http://www.cnblogs.com/kerrycode/p/3595724.html 当我觉得对Linux系统下修改hostname已经非常熟悉的时候,今天碰到了几个个问题,这几个问题 ...
- mapreduce精简概括--转
mapreduce精简概括 We want to count all the books in the library. You count up shelf #1, I count up shelf ...