입출력
풀이
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
bool compare(pair<int, string> a, pair<int, string> b)
{
return a.first < b.first;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int cnt;
cin >> cnt;
int age;
string name;
vector<pair<int, string>> info;
for (int i = 0; i < cnt; i++)
{
cin >> age >> name;
info.push_back({ age, name });
}
stable_sort(info.begin(), info.end(), compare);
cout << "------------" << "Stable sort compare" << "------------" << endl;
for (int i = 0; i < cnt; i++)
cout << info[i].first << " " << info[i].second << "\n";
/// ///////////////////////////////////////////////////////
/*sort(info.begin(), info.end());
cout << "------------" << "sort" << "------------" << endl;
for (int i = 0; i < cnt; i++)
cout << info[i].first << " " << info[i].second << "\n";
/// ///////////////////////////////////////////////////////
sort(info.begin(), info.end(), compare);
cout << "------------" << "sort compare" << "------------" << endl;
for (int i = 0; i < cnt; i++)
cout << info[i].first << " " << info[i].second << "\n";
/// ///////////////////////////////////////////////////////
stable_sort(info.begin(), info.end());
cout << "------------" << "stable sort" << "------------" << endl;
for (int i = 0; i < cnt; i++)
cout << info[i].first << " " << info[i].second << "\n";*/
}
stable_sort() 는 안정된 정렬을 보장한다
따라서 비교하는 값이 같은 경우에는 입력된 순서대로 출력한다
'> 코딩테스트' 카테고리의 다른 글
[백준] 14425 문자열 집합 (C++) (0) | 2023.06.13 |
---|---|
[백준] 10815 숫자 카드 (C++) (1) | 2023.06.11 |
[백준] 1181 단어 정렬 (C++) (0) | 2023.06.06 |
[백준] 11651 좌표 정렬하기2 (C++) (0) | 2023.06.06 |
[백준] 11650 좌표 정렬하기 (C++) (0) | 2023.06.04 |