함수 호출 연산자 () 오버로딩 Function Call Operator () Overloading in C++
C++에서는 객체또는 구조체의 이름만으로 호출가능한 함수를 만들 수 있다.
그에 대한 개념은 위 블로그에 작성되어있다.
c++ priority queue 예제 : compare 구조체만 잘 정의합시다.
PriorityQueue 를 만들때 3번쨰 인자는 자료형의 우선순의를 비교할 수 있는 함수호출연산자 즉, 구조체(혹은 객체)의 이름을 넣어야한다.
아래는 Kruskal Algorithm을 구현할 때 edge의 비교함수를 만든 부분이다.
class cmp {
public:
bool operator()(edge& a, edge& b) {
return a.weight > b.weight;
}
};
struct cmp {
bool operator()(edge& a, edge& b) {
return a.weight > b.weight;
}
};
priority_queue<edge, vector<edge>, cmp> pq;