C언어/문제풀다 하나씩
#include <algorithm>의 sort&compare함수 쓰기 - 사정정렬
mcdn
2020. 5. 13. 17:05
반응형
#include <algorithm>의 sort&compare함수 쓰기
bool compare
string a string b
맨 마지막에 return false빼먹지 말기!
저거 빼먹어서 에러남

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
string t[20];
bool compare(string a, string b) {
int lena = a.length();
int lenb = b.length();
if (lena < lenb) return true;
if (lena > lenb) return false;
if (a < b) return true;
return false;
}
int main(){
int n;
cin >> n;
for (int i = 0;i < n;i++) {
cin >> t[i];
}
sort(&t[0], &t[n], compare);
for (int i = 0;i < n;i++) {
cout << t[i] << endl;
}
}
sort ( &t[0] & t[n],compare)
할때 t[n+1]이라 씀 ㅋㅋㅋ 아니야!
배열 크기 설정하는건 원래도 하나 더 큰 거 잊지 않기

입력 예제
7 ABC B TTS FRIENDS TRUE LOVE MORETIM
출력 결과
B ABC TTS LOVE TRUE FRIENDS MORETIM
반응형