실제 코딩테스트에서 정렬을 만들어서 쓰는 경우는 거의 없다.
개념적으로만 익혀두고 실제로는 STL의 Sort라이브러리를 쓰면 된다.
[ 사용 ]
( 기본적인 사용법 )
#include <iostream> #include <algorithm> using namespace std; int main(void){ int a[10] = {9, 3, 5, 4, 1, 10, 8, 6, 7, 2}; sort(a, a+10); // sort 사용! }
( 정해진 조건에 의한 정렬 )
#include <iostream> #include <algorithm> using namespace std; bool compare(int a, int b){ return a > b; } int main(void){ int a[10] = {9, 3, 5, 4, 1, 10, 8, 6, 7, 2}; sort(a, a+10, compare); // 조건에 따라 내림차순 정렬이 수행된다 }
( 데이터를 묶어서 정렬하는 방법 ) - 실무에 가까운 사용 방법 - 코테는 pair사용!
#include <iostream> #include <algorithm> using namespace std; class Student{ public: string name; int score; Student(string name, int score){ this->name = name; this->score = score; } // 정렬 기준은 '점수가 작은 순서' bool operator < (const Student &student){ return this->score < student.score; } }; --- int main(){ Student students[] = { Student("김정욱", 93), Student("한재현", 92), Student("강병헌", 91), Student("이현종", 87), Student("이진호", 94), }; sort(students, students+5); for (auto a : students){ cout << a.name << endl; } }
( 데이터를 묶어서 정렬하는 방법 ) - 코테에 적합한 방법 (pair이용!)
#include <iostream> #include <algorithm> #include <vector> using namespace std; int main(){ vector <pair<int, string>> v; v.push_back({95, "김정욱"}); v.push_back({91, "한재현"}); v.push_back({94, "김가영"}); v.push_back({93, "주주베"}); sort(v.begin(), v.end()); for(auto a : v){ cout << a.second << ' '; } }
( pair에 compare 조건을 달아서 sort )
#include <iostream> #include <algorithm> #include <vector> using namespace std; bool compare(pair<string, pair<int, int>> a, pair<string, pair<int, int>> b){ if(a.second.first == b.second.first){ return a.second.second > b.second.second; }else{ return a.second.first > b.second.first; } } int main(){ vector <pair<string, pair<int, int>>> v; v.push_back({"김정욱", {91, 19960808}}); v.push_back({"한재현", {93, 19960921}}); v.push_back({"김가영", {91, 19960807}}); v.push_back({"주주베", {96, 19961011}}); sort(v.begin(), v.end(), compare); for(auto a : v){ cout << a.first << ' '; } }
: STL sort()를 이용한 정렬 문제
#include <iostream> #include <algorithm> using namespace std; bool compare(string a, string b){ if(a.length() < b.length()){ return 1; }else if(a.length() > b.length()){ return 0; }else{ return a < b; } } int main() { int N; cin >> N; string str[20002]; for(int i=0;i<N;i++){ cin >> str[i]; } sort(str, str+N, compare); for(int i=0;i<N;i++) { if(i>0 && str[i-1] == str[i]){ continue; }else{ cout << str[i] <<'\n'; } } }
c++ STL sort를 이용한 정렬문제(2)
[ 코드 ]
#include <iostream> #include <algorithm> using namespace std; bool compare(string a, string b){ if(a.length() < b.length()){ return 1; }else if(a.length() > b.length()){ return 0; } int sumA=0, sumB=0; for(auto i : a){ if(i >= '0' && i <= '9'){ sumA+= i-'0'; } } for(auto i : b){ if(i >= '0' && i <= '9'){ sumB+= i-'0'; } } if(sumA < sumB){ return 1; }else if(sumA > sumB){ return 0; }else{ return a < b; } } int main() { int N; cin >> N; string str[1002]; for(int i=0;i<N;i++){ cin >> str[i]; } sort(str, str+N, compare); for(int i=0;i<N;i++){ cout << str[i] << '\n'; } }