강의로 돌아가기
Se***

std::sort segmentation fault

해당 코드로 segfault가 뜨는데 이유를 알 수 있을까요?

작성중인 코드―solution.cpp
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
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

bool cmp(vector<int> &a, vector<int> &b){
    return a[0] >= b[0];
}

int solution(vector<vector<int>> scores) {
    int answer = 1;
    int w = scores[0][0] + scores[0][1];
    vector<int> wanho(scores[0]);
    sort(scores.begin(), scores.end(), cmp);
    int max = 0;
    for (vector<int> v : scores) {
        if (v[0] > wanho[0] && v[1] > wanho[1])
            return -1;
        if (v[0] + v[1] > w && max <= v[1]){
                answer++;
                max = v[1];
        }
    }
    return answer;
}
  • Lo***

    제가 시도해보지 않았지만 sort 의 비교함수는 순약순서 (strict weak ordering) 을 만족해야 하므로 a op a 는 false 가 되어야 하고, a op b 와 b op a 결과가 둘다 true 가 되어서는 안 되는 것으로 알고 있습니다. 프로그래머스는 C++17 으로 운영되는 모양이지만 C++20 부터는 <=> 연산이 도입되어서 어떻게 되는지 모르겠네요.

    Lo***―2023.01.28 16:03
0 개의 답변
답변 쓰기
이 입력폼은 마크다운 문법을 지원합니다.