-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstd_sort.cpp
More file actions
51 lines (41 loc) · 1.1 KB
/
std_sort.cpp
File metadata and controls
51 lines (41 loc) · 1.1 KB
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
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
class Stu {
public:
Stu() {
}
Stu(int age, std::string name) : m_age(age), m_name(name) {
}
~Stu() {
}
public:
int m_age;
std::string m_name;
};
bool compare_stu(Stu ele1, Stu ele2) {
return (ele1.m_age > ele2.m_age);
}
int main(int argc, char* argv[]) {
Stu stu1(23, "zhangsan");
Stu stu2(31, "alice");
Stu stu3(57, "jack");
Stu stu4(12, "bob");
Stu stu5(38, "wangwu");
std::vector<Stu> stus;
stus.push_back(stu1);
stus.push_back(stu2);
stus.push_back(stu3);
stus.push_back(stu4);
stus.push_back(stu5);
for (std::vector<Stu>::iterator it = stus.begin(); it != stus.end(); it++) {
std::cout << it->m_age << ", " << it->m_name << std::endl;
}
std::cout << "-----------------------------------------------" << std::endl;
std::sort(stus.begin(), stus.end(), compare_stu);
for (std::vector<Stu>::iterator it = stus.begin(); it != stus.end(); it++) {
std::cout << it->m_age << ", " << it->m_name << std::endl;
}
return 0;
}