C语言sortabc函数 c语言 sort函数( 三 )


{
return *(double *)a*(double *)b ? 1 : -1;
}
qsort(in,100,sizeof(in[0]),cmp);
sort函数的用法:对结构体一级排序
?
1
2
3
4
5
6
7
8
9
10
11
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 ;
}
qsort(s,100,sizeof(s[0]),cmp);
sort函数的用法:对结构体
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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);
sort函数的用法:对字符串进行排序
?
1
2
3
4
5
6
7
8
9
10
11
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);
sort函数的用法:计算几何中求凸包的cmp
?
1
2
3
4
5
6
7
8
9
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;
}
猜你喜欢:
1. c中的用法
2. c语言中逻辑或的用法
3. c语言strcmp的用法
4. c语言中free的用法
5. c语言pow的用法
6. c语言中putchar的用法
C语言sort函数如何使用#includestdio.h
#includestdlib.h
int comp(const void*a,const void*b)//用来做比较的函数 。
{
return *(int*)a-*(int*)b;
}
int main()
{
int a[10] = {2,4,1,5,5,3,7,4,1,5};//乱序的数组 。
int i;
qsort(a,10,sizeof(int),comp);//调用qsort排序
for(i=0;i10;i++)//输出排序后的数组
{
printf("%d ",a[i]);
}
return 0;
}
c语言和c++中,对于sort函数的使用,不同 。c语言中没有预置的sort函数 , 如果在c语言中,要调用sort函数,就需要自定义一个用于排序的函数 , 或者使用c语言自有的qsort函数,其头文件为stdlib.h 。
C语言怎么使用sort函数,求举个简单的例子,谢谢sort()函数描述C语言sortabc函数:对给定区间所有元素进行排序 。
sort()函数语法C语言sortabc函数:sort(begin,end)C语言sortabc函数,表示一个范围 。
sort()函数举例C语言sortabc函数:
int _tmain(int argc, _TCHAR* argv[])
{
int a[20]={2,4,1,23,5,76,0,43,24,65},i;
for(i=0;i20;i++)
couta[i]endl;
sort(a,a+20);
for(i=0;i20;i++)
couta[i]endl;
return 0;
}
输出结果将是把数组a按升序排序 。
c语言中的sort函数怎么使用,请举出一个实例,非常感谢!#include algorithm//需要包含这个头文件
#include iostream
using namespace std;
int main(void)