- #include<bits/stdc++.h>
- using namespace std;
- struct student
- {
- string name;
- int score;
- };
- bool compare(student a, student b)
- {
- return a.score>b.score;
- }
- student st[4];
- int main()
- {
- st[0].name="Bob";
- st[1].name="Cindy";
- st[2].name="Alice";
- st[3].name="Mary";
- st[0].score=70;
- st[1].score=66;
- st[2].score=77;
- st[3].score=76;
- for(student s: st)
- cout<<s.name<<"\t"<<s.score<<" 分"<<endl;
- sort(st, st+4, compare);
- for(student s: st)
- cout<<s.name<<"\t"<<s.score<<" 分"<<endl;
- return 0;
- }
複製代碼 |