반응형
C++ 비교함수 활용 여러 인자 비교하기 - 백준 오답노트 10825
1.make_pair 는 한 쌍의 데이터를 같는데 pair 끼리 대소연산은 첫번째 원소를 비교하고 같으면 두번째 원소를 비교한다.
make_pair의 두번쨰 원소에도 계속 pair을 넣어주면 계속해서 비교 연산이 가능하다.
예시) 오름차순 정렬 - 한국어 점수가 낮은순서, 국어점수가 같다면, 영어가 높은순서, 같다면, 수학이 낮은순서, 같다면 이름순
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 | //10825 #include<iostream> #include<algorithm> #include<vector> #include<string> using namespace std; class person{ public: string name; int kor; int eng; int math; public: person(string _name,int _kor,int _eng,int _math) : name(_name),kor(_kor),eng(_eng),math(_math) { // this->name = name; // this->kor = kor; // this->eng = eng; // this->math = math; } }; bool cmp(const person& u,const person&v){ return make_pair(-u.kor,make_pair(u.eng,make_pair(-u.math,u.name))) < make_pair(-v.kor,make_pair(v.eng,make_pair(-v.math,v.name))); } int main(){ int n; cin>>n; vector<person> persons; while(n--){ string t1;int t2,t3,t4; cin>>t1; cin>>t2>>t3>>t4; persons.push_back(person(t1,t2,t3,t4)); } sort(persons.begin(),persons.end(),cmp); for(auto a:persons){ cout<<a.name<<"\n"; } return 0; } //FB1. 강력하게 틀린 직감 : 증가하는 순서 = 오름차순 = return u < v; u 가 v보다 작으면 참 투르 ~ //FB2. 맴버 이니셜 라이져 + public : 구역쓰 ~ //FB3. 비교함수에 const 할것 ! | cs |
반응형
'알고리즘 > 백준[BOJ] 오답노트' 카테고리의 다른 글
C 반복for while 문 탈출 break냐 return - 백준 알고리즘 9012 (0) | 2019.03.02 |
---|---|
C scanf의 리턴값 무한루프 - 백준 알고리즘 오답노트 11721 (0) | 2019.03.02 |
C++ vector에 중복된 인자 제거하기 - 백준 알고리즘 오답노트 1181 (0) | 2019.03.02 |
C++ rotate 함수 - 백준 알고리즘 오답노트 10812 (0) | 2019.03.02 |
C++ 파일의 끝 직접 입력하기 - 백준 알고리즘 오답노트 1152 (0) | 2019.03.02 |