vector<pair<string, int>> v(m.begin(), m.end());
3-2. 커스텀 정렬 조건 구현
- 문자열 길이가 작은 순으로
- 문자열 길이가 같다면 사전 순으로
bool cmp(pair<string, int> &a, pair<string, int> &b) {
if(a.second==b.second) { //사전 순으로
return a.first < b.first;
}
return a.second < b.second; //문자열 길이 순으로
}
정답 코드
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
int n;
map<string, int> m;
bool cmp(pair<string, int> &a, pair<string, int> &b) {
if(a.second==b.second) {
return a.first < b.first;
}
return a.second < b.second;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n;
for(int i=0; i<n; i++) {
string in;
cin>>in;
m.insert({in, in.size()});
}
vector<pair<string, int>> v(m.begin(), m.end());
sort(v.begin(), v.end(), cmp);
for(auto a: v) {
cout<<a.first<<'\n';
}
return 0;
}