Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- 알고리즘
- 구현
- 개발
- 브루트포스
- 스택
- cs
- 백준
- 그래프
- 컴공과
- 코테
- coding
- DP
- 정석
- 컴공
- 오퍼레이팅시스템
- 컴퓨터공학과
- 정석학술정보관
- 코딩
- 북리뷰
- Computer science
- Operating System
- vector
- 너비우선탐색
- c++
- OS
- Stack
- 자료구조
- 오에스
- bfs
- 문제풀이
Archives
- Today
- Total
Little Jay
[C++] 백준 10814번 나이순 정렬 본문
단순히 sort 만 쓰면 당연하게도 틀리는 문
이 문제때문에 처음으로 stable_sort 를 써보았다.
stable_sort가 정리되어있는 www.cplusplus.com/reference/algorithm/stable_sort/
stable_sort - C++ Reference
function template std::stable_sort template void stable_sort ( RandomAccessIterator first, RandomAccessIterator last ); template void stable_sort ( RandomAccessIterator first, RandomAccessIterator last, Compare comp ); Sort elements preserving
www.cplusplus.com
여기랑 여러 블로그를 참조했다.
stable_sort 를 통해서 vector pair의 second를 조절할 수 있다는 것.
다시한번 공부하고 넘어가자.
#include <iostream>
#include <vector>
#include <string>
#include <utility>
#include <algorithm>
using namespace std;
bool cmp(pair<int, string> u, pair<int, string> v) {
return u.first < v.first;
}
int main() {
int testCase;
cin >> testCase;
vector<pair<int, string>> v;
int age;
string name;
for (int i = 0; i < testCase; i++) {
cin >> age >> name;
v.push_back(make_pair(age, name));
}
//sort(v.begin(), v.end()); 이렇게 정렬시키면 내가 원하는대로 입력순 출력이 불가
stable_sort(v.begin(), v.end(), cmp);
for (int i = 0; i < testCase; i++) {
cout << v[i].first << " " << v[i].second << "\n";
}
v.clear();
return 0;
}'알고리즘 > BOJ' 카테고리의 다른 글
| [C++] 백준 10809번 알파벳 찾기 (0) | 2021.03.15 |
|---|---|
| [C++] 백준 2577번 숫자의 개수 (0) | 2021.03.14 |
| [C++] 백준 10250번 ACM호텔 (0) | 2021.03.11 |
| [C++] 백준 1085번 직사각형에서 탈출 (0) | 2021.03.11 |
| [C++] 백준 2751번 수 정렬하기 2 (0) | 2021.03.11 |
Comments