-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestclassorder.cpp
More file actions
40 lines (35 loc) · 881 Bytes
/
testclassorder.cpp
File metadata and controls
40 lines (35 loc) · 881 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
struct Student{
string name;
int score;
bool operator <(const Student &s) const{
if (score == s.score) return name < s.name;
return score < s.score;
}
};
Student students[] = {
{"Amy", 69},
{"Jack", 100},
{"Bob", 69}
};
bool comp(const Student&a, const Student&b)
{
if (a.score == b.score) return a.name < b.name;
return a.score > b.score;
}
int main()
{
int n = sizeof(students)/sizeof(Student);
sort(students, students+n, comp);
cout << "Descending:" << endl;
for (auto& s: students)
cout << s.name << " " << s.score << endl;
sort(students, students+n);
cout << "Ascending:" << endl;
for (auto& s: students)
cout << s.name << " " << s.score << endl;
return 0;
}