๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’Ž/C++

[C++] priority_queue ์šฐ์„ ์ˆœ์œ„ ํ

by dar0m! 2020. 3. 25.
  • ๋‚ด๋ฆผ์ฐจ์ˆœ less<>๊ฐ€ default
  • ๋งจ ์ฒซ๋ฒˆ์งธ ์ธ์ž๋Š” pq.top() ์œผ๋กœ ์ ‘๊ทผ
  • X๋Š” ์ž๋ฃŒํ˜•์ผ ๋•Œ, ์˜ค๋ฆ„์ฐจ์ˆœ ์šฐ์„ ์ˆœ์œ„ ํ๋Š”

    ๐Ÿ‘‰ priority_queue<X, vector<X>, greater<X>> pq;
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <queue>
using namespace std;
typedef pair<intint> p;
 
priority_queue<int> pq;        // ๋‚ด๋ฆผ์ฐจ์ˆœ default
 
priority_queue<intvector<int>, less<int>> pq1;           // ๋‚ด๋ฆผ์ฐจ์ˆœ default
 
priority_queue<intvector<int>, greater<int>> pq2;        // ์˜ค๋ฆ„์ฐจ์ˆœ
 
// priority_queue<X, vector<X>, greater<X>> pq3;            X๋Š” ์ž๋ฃŒํ˜•, pair๋„ ์ž๋™์ •๋ ฌ
priority_queue<pair<int, p>vector<pair<int, p>>, greater<pair<int, p>>> pq3; // ์˜ค๋ฆ„์ฐจ์ˆœ
int main() {
 
    pq.push(9);                            // 9 8 7 5 4 1
    pq.push(4);
    pq.push(5);
    pq.push(7);
    pq.push(8);
    pq.push(1);
    while (!pq.empty()) {
        printf("%d ", pq.top());        // queue ์™€ ๋‹ค๋ฅด๊ฒŒ q.front()๊ฐ€ ์•„๋‹ˆ๋ผ pq.top();
        pq.pop();
    }
    printf("\n");
 
    pq1.push(9);                            // 9 8 7 5 4 1
    pq1.push(4);
    pq1.push(5);
    pq1.push(7);
    pq1.push(8);
    pq1.push(1);
    while (!pq1.empty()) {
        printf("%d ", pq1.top());
        pq1.pop();
    }
    printf("\n");
 
    pq2.push(9);                            // 1 4 5 7 8 9
    pq2.push(4);
    pq2.push(5);
    pq2.push(7);
    pq2.push(8);
    pq2.push(1);
    while (!pq2.empty()) {
        printf("%d ", pq2.top());
        pq2.pop();
    }
    printf("\n");
 
    pq3.push({ 1, {11} });                // 1 1 1        
    pq3.push({ 9, {11} });                // 5 2 3
    pq3.push({ 5, {43} });                // 5 4 2
    pq3.push({ 5, {42} });                // 5 4 3
    pq3.push({ 8, {21} });                // 8 0 1
    pq3.push({ 8, {01} });                // 8 2 1
    pq3.push({ 5, {23} });                // 9 1 1
    while (!pq3.empty()) {
        printf("%d %d %d\n", pq3.top().first, pq3.top().second.first, pq3.top().second.second);
        pq3.pop();
    }
 
    return 0;
}
cs

๋Œ“๊ธ€