devLupin / algorithm

PS
1 stars 0 forks source link

priority_queue #11

Open devLupin opened 1 year ago

devLupin commented 1 year ago

priority_queue(pq)는 자료구조 중 하나로, C++에서는 queue에 구현되어 있음.

priority_queue<int> maxPQ;
priority_queue<int, vector<int>, greater<int>> minPQ;

// 다음과 같이 대체 가능하다.
priority_queue<int> pq;

pq.push(-num);
-pq.top();

// 그러나 마이너스 보다는 bitwise-not
pq.push(~num);
~pq.top();
devLupin commented 1 year ago

Operator overoading

typedef struct Compare{
    bool operator()(int a, int b) {
        return a > b;
    }
};

priority_queue<int, vector<int>, Compare> pq;
typedef struct Node {
    int x, y;

    Node(int x, int y) { this->x = x, this->y = y; }

    bool operator<(const Node& tmp) const {
        if (x == tmp.x)
            return y < tmp.y;
        return x < tmp.x;
    }
};

priority_queue<Node> pq;