您的位置:首页 > 其它

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: