본문 바로가기

Study/C++

priority_queue에 struct를 넣을 때는 연산자 오버로딩이 필요하다

 

 

 

 

연산자 오버로딩이 없으면 위와 같은 에러가 난다.

priority를 정해줄 수 없어서 그런 것 같다.

 

`operator<`를 오버로딩해주고 해결했다.

 

#코드

 

struct node {
    int n;
    int m;
    int cost;
};

//overloading 꼭 해야함
//내림차순으로 리턴
bool operator<(node a, node b) {
    return a.cost > b.cost;
}

//board[n][m]으로 가기 위한 최소비용 리턴
int bfs(int n, int m){
    //{비용, [세로좌표, 가로좌표]}
    priority_queue<node> q;
    struct node start = {1,1,0};
    q.push(start);