Closed cherry-go-round closed 5 months ago
bool compare(pair<string, int> &p1, pair<string, int> &p2) {
if (p1.second != p2.second) return p1.second > p2.second;
if (p1.first.length() != p2.first.length()) return p1.first.length() > p2.first.length();
return p1.first < p2.first;
}
compare 함수 조건문을 어떻게 설정하냐에 따라 문제가 발생하는거 같아요
비교연산자 쓰니까 segfault가 덜 나오기는 하는데 가끔 터지는 segfault는 왜 그런지는 모르겠네요..ㅠ
sort(
v.begin(),
v.end(),
[&](pair<string, int> p1, pair<string, int> p2) {
if (p1.second != p2.second) return p1.second > p2.second;
if (p1.first.length() != p2.first.length()) return p1.first.length() > p2.first.length();
return p1.first < p2.first;
}
);
이렇게 적으니까 통과됐습니다! bool 함수를 전달했어야 했네요. 아마 0이나 1이 나와야 하는 상황에서 다른 값이 나오니까 문제가 되었던 것 같아요. 그런데 동작이 잘못되는 것은 이해가 되는데 왜 segmentation fault인지는 잘 모르겠네요.😅
자바로 통과한 코드입니다.
원래 C++코드입니다. 어떤 부분이 잘못되었을까요? 테스트케이스 중 일부는 통과하는데 진행 중에 segfault가 발생합니다.