Skip to content

Commit c1bf5e9

Browse files
committed
algorithm header Sort() use
1 parent 4f38f7d commit c1bf5e9

6 files changed

+88
-0
lines changed

Algorithm_sort.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include<iostream>
2+
#include<algorithm>
3+
4+
using namespace std;
5+
6+
class Student{
7+
public:
8+
string name;
9+
int score;
10+
Student(string name, int score){
11+
this -> name = name;
12+
this -> score = score;
13+
}
14+
// 정렬 기준 '점수가 작은 순서'
15+
bool operator <(Student &student){
16+
return this -> score > student.score;
17+
}
18+
};
19+
20+
int main(void){
21+
Student students[] = {
22+
Student("JSH", 99),
23+
Student("LHJ", 95),
24+
Student("KMJ", 97),
25+
Student("HMS", 91),
26+
Student("OHJ", 93)
27+
};
28+
sort(students, students+5);
29+
for(int i =0; i<5; i++){
30+
cout << students[i].name << ' ';
31+
}
32+
}

Algorithm_sort.exe

2.41 MB
Binary file not shown.

Algorithm_sort2.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include<iostream>
2+
#include<vector>
3+
#include<algorithm>
4+
5+
using namespace std;
6+
7+
int main(void){
8+
// pair를 통해서 int 와 string 을 묶어줌
9+
vector<pair<int, string>> v;
10+
v.push_back(pair<int, string>(90, "90 JSH"));
11+
v.push_back(pair<int, string>(69, "69 HMS"));
12+
v.push_back(pair<int, string>(85, "85 SSH"));
13+
v.push_back(pair<int, string>(70, "70 KMJ"));
14+
v.push_back(pair<int, string>(75, "75 OHJ"));
15+
16+
sort(v.begin(), v.end());
17+
18+
for(int i =0; i <v.size(); i++){
19+
cout << v[i].second << ' ' <<"\n";
20+
}
21+
22+
return 0;
23+
}

Algorithm_sort2.exe

2.52 MB
Binary file not shown.

Algorithm_sort3.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include<iostream>
2+
#include<vector>
3+
#include<algorithm>
4+
5+
using namespace std;
6+
7+
bool compare(pair<string,pair<int, int>> a,
8+
pair<string,pair<int, int>> b){
9+
if(a.second.first == b.second.first){
10+
return a.second.second > b.second.second;
11+
}
12+
else{
13+
return a.second.first > b.second.first;
14+
}
15+
}
16+
17+
int main(void){
18+
// 이중pair를 통해서 세 변수를 묶어줌
19+
vector<pair<string,pair<int, int>>> v;
20+
v.push_back(pair<string,pair<int, int>>("90 JSH", pair<int,int>(90, 19961222)));
21+
v.push_back(pair<string,pair<int, int>>("69 HMS", pair<int,int>(69, 19931222)));
22+
v.push_back(pair<string,pair<int, int>>("85 SSH", pair<int,int>(85, 19971222)));
23+
v.push_back(pair<string,pair<int, int>>("70 KMJ", pair<int,int>(70, 19981222)));
24+
v.push_back(pair<string,pair<int, int>>("70 OHJ", pair<int,int>(70, 19991222)));
25+
26+
sort(v.begin(), v.end(), compare);
27+
28+
for(int i =0; i <v.size(); i++){
29+
cout << v[i].first << ' ' <<"\n";
30+
}
31+
32+
return 0;
33+
}

Algorithm_sort3.exe

2.54 MB
Binary file not shown.

0 commit comments

Comments
 (0)