C++ Sort
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
bool compare(int i, int j){
return i > j;
}
class Student {
public:
string name;
int score;
Student(string name, int score){
this->name = name;
this->score = score;
}
bool operator <(Student &student){
return this->score < student.score;
}
};
int main() {
cin.tie(0); cout.tie(0);
ios::sync_with_stdio(0);
// 기본적으로 이터레이터 범위, compare 파라미터로 콜백함수를 선택적으로 처리
// 기본정렬은 오름차순이다
vector<int> v = {3,2,1,6,5,8,9,7,4};
sort(v.begin(),v.end());
for(auto &e:v){
cout << e << " ";
}
cout << "\n";
sort(v.begin(),v.end(),compare);
for(auto &e:v){
cout << e << " ";
}
cout << "\n";
sort(v.begin(),v.end(),greater<>()); // less<>()
for(auto &e:v){
cout << e << " ";
}
cout << "\n";
Student students[] = {
Student("a",93),
Student("b",90),
Student("c",97)
};
sort(students,students+3);
for(auto &e:students){
cout << e.name << " " << e.score << "\n";
}
cout << "\n";
return 0;
}
/*
1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1
9 8 7 6 5 4 3 2 1
b 90
a 93
c 97
*/
This post is licensed under CC BY 4.0 by the author.