qsort和sort对结构体的排序
2016-07-26 11:17
344 查看
struct node { int x, y; int time; } N[100]; int cmp1(const void *a, const void *b) { //按x的大小从小到大排序 return (*(struct node *)a).x - (*(struct node*)b).x ; } int cmp3(const void *p1, const void *p2) { //先按x的大小从小到大排序,如果x相等,则按y的大小排序 struct node *a = (node *)p1; struct node *b = (node *)p2; if(a->x == b->x) return b->y - a->y ; else return b->x - a->x ; } bool cmp2(const struct node &a, const struct node &b) { //按x的大小从小到大排序 return a.x < b.y; } int main() { qsort(N, 100, sizeof (N[0]), cmp1); qsort(N, 100, sizeof (N[0]), cmp3); sort(N, N+100, cmp2); return 0; }
来自hnust_dengzhixiang
相关文章推荐
- iOS音频篇:AVPlayer的缓存实现
- 切换页面隐藏导航栏出现黑条的问题
- MFC圆角矩形窗口
- 我仅代表还是我谨代表
- 从Arm汇编看Android C++虚函数实现原理
- Debug---Eclipse断点调试基础
- 欢迎使用CSDN-markdown编辑器
- 自动调整linux系统时间和时区与Internet时间同步
- Python 练习实例21
- Blueprints - 蓝图之间的交互
- CentOS 6.8安装 glibc 2.15
- 数据抓取常用:python时间转换
- POJ2594
- 正则表达式简单汇总
- 深入理解javascript函数系列第二篇——函数参数
- memcache的学习路线图
- 关于Android SQLite3多线程并发问题,学习笔记。
- 欢迎使用CSDN-markdown编辑器
- Codeforces Round #305 (Div. 1) B. Mike and Feet(思路)
- JavaScript将对象转换为JSON