sort 对结构体排序

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 const int MAXN = 40010;
 5 int N;
 6 struct Point
 7 {
 8     int x;
 9     bool operator< (const Point& T) const
10     {
11         return x < T.x;
12     }
13 }P[MAXN];
14 
15 struct Points
16 {
17     int x;
18 }Ps[MAXN];
19 
20 
21 bool cmp(Points a,Points b)
22 {
23     return a.x < b.x;
24 }
25 
26 int main()
27 {
28     while (~scanf("%d",&N) && N)
29     {
30         for (int i(0);i < N;++i)
31         {
32             scanf("%d",&P[i].x);
33             Ps[i].x = P[i].x;
34         }
35         //自定义排序方式一
36         std::sort(P,P+N);
37         //自定义排序方式二
38         std::sort(Ps,Ps+N,cmp);
39     }
40     return 0;
41 }

 

posted @ 2013-08-20 16:41  CNKylin  阅读(341)  评论(0编辑  收藏  举报