system(pause);

  1. int cmp(const void *a ,const void *b)
  2. {
  3. return *(int *)a - *(int *)b ;          //从小到大排序,把a,b位置反过来就是从大到小
  4. }

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的更多相关文章

  1. stdlib.h stdio.h

    stdlib.h 即standard library标准库头文件.stdlib.h里面定义了五种类型.一些宏和通用工具函数. 类型例如size_t.wchar_t.div_t.ldiv_t和lldiv ...

  2. 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 ...

  3. System.TypeInitializationException: The type initializer for 'Mono.Unix.Native.Stdlib' threw an exception.

    08-31 17:02:03: ### DEBUG ##########################System.TypeInitializationException: The type ini ...

  4. stdlib 头文件

    stdlib 头文件即standard library标准库头文件.stdlib.h里面定义了五种类型.一些宏和通用工具函数. 类型例如size_t.wchar_t.div_t.ldiv_t和lldi ...

  5. stdlib.h 头文件

    stdlib 头文件即standard library标准库头文件.stdlib.h里面定义了五种类型.一些宏和通用工具函数. 类型例如size_t.wchar_t.div_t.ldiv_t和lldi ...

  6. c语言实用功能库函数#include<stdlib.h>

    实用函数<stdlib.h> 在头文件<stdlib.h>中说明了用于数值转换.内存分配以及具有其他相似任务的函数. 1 atof #include <stdlib.h& ...

  7. #include <stdlib.h>

    1 _itoa 2 atoi 3 rand() 4 srand 1 _itoa _itoa(int value,char*string,int radix); int value 被转换的整数,cha ...

  8. mac 下 clang++ 找不到头文件 stdlib.h

    因为要用 openmp库,用 clang++ 编译 c++程序,出现了如下报错: clang++ xx.cpp -o xx -fopenmp /usr/local/Cellar/llvm/7.0.0/ ...

  9. C语言中你可能不熟悉的头文件(stdlib.h)

    C语言中你可能不熟悉的头文件<cstdlib>(stdlib.h) C Standard General Utilities Library (header) C标准通用工具库(头文件) ...

随机推荐

  1. 《苹果开发之Cocoa编程》挑战2 创建一个数据源 练习

    <苹果开发之Cocoa编程>第4版 P87 创建一个to-do list应用程序,在文本框中输入任务.当用户单击Add按钮时,添加字符串到一个变长队列,新任务就出现在list的末尾. 关键 ...

  2. 学习笔记之 初试Linux遇到的问题 2015-10-13

    1. 安装.deb文件,用sudo gdebi XXX.deb sudo apt-get install xxx 2. 需要配置系统路径: LD_LIBRARY_PATH=.../lib:LD_LIB ...

  3. 最大化 AIX 上的 Java 性能,第 4 部分: 监视流量

    http://www.ibm.com/developerworks/cn/aix/library/es-Javaperf/es-Javaperf4.html 最大化 AIX 上的 Java 性能,第 ...

  4. ABBYY FineReader的图像编辑器功能使用方法

    日常工作中,有时可能需要对图像进行编辑,可是正常情况下大家都知道图像是不能直接编辑的,需要借助工具.ABBYY FineReader 12 OCR文字识别软件可以实现图像的手动编辑,接下来就具体给大家 ...

  5. ASP.NET MVC内置的Filter实现介绍

    有时候你想在调用action方法之前或者action方法之后处理一些逻辑,为了支持这个,ASP.NET MVC允许你创建action过滤器.Action过滤器是自定义的Attributes,用来标记添 ...

  6. bugFree与zentao

    架构:b/s 开发语言:php 服务器端:lamp 客户端:浏览器 相关包:lamp BugFree是借鉴微软的研发流程和Bug管理理念,使用PHP+MySQL独立写出的一个Bug管理系统.简单实用. ...

  7. shell之变量与read

    环境变量 set 环境变量可供shell以外的程序使用 shell变量 env shell变量仅供shell内部使用 set:显示(设置)shell变量 包括的私有变量以及用户变量,不同类的shell ...

  8. 淘宝天猫关键词SEO优化

    淘宝天猫的网站完全像是一个成熟的搜索引擎,只是从google.bing.baidu改成了淘宝天猫而已,普通搜索引擎有品专,有皇冠,有PC,有无线:淘宝天猫里面有钻展,有直通车,也有PC,无线.搜索引擎 ...

  9. python数据类型之 set

    set是一个无序且不重复的元素集合 class set(object): """ set() -> new empty set object set(iterabl ...

  10. Android 如何全局获取Context

    有时,在处理业务逻辑的时候,需要Context对象,但在某些情况下,并非容易获取,这时就需要一些巧妙的手段来管理Context. 在Android中,提供了一个类Application,当应用程序启动 ...