六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 31|回复: 0

利用c函数qsort对结构体进行排序

[复制链接]

升级  28%

26

主题

26

主题

26

主题

秀才

Rank: 2

积分
92
 楼主| 发表于 2013-1-26 12:44:13 | 显示全部楼层 |阅读模式
#include <stdio.h>#include <stdlib.h>typedef struct{int x;int y;int h;}dot;int cmp(const void *a, const void *b){return (*(dot *)a).h - (*(dot *)b).h;}int main(){dot line[9];line[0].h = 30;line[1].h = 2;line[2].h = -10;line[3].h = 11;line[4].h = 20;line[5].h = 100;line[6].h = 90;line[7].h = 0;line[8].h = -1;int i;for(i = 0; i< 9; i++)printf("%d ", line[i].h);printf("\n");qsort(line, 9, sizeof(dot), cmp);for(i = 0; i< 9; i++)printf("%d ", line[i].h);printf("\n");return 0;}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表