priority_queue を使ってみる (C++)
C++
Published: 2019-12-30

やったこと

priority_queue を使ってみます。

確認環境

$ g++ --version
g++ (Homebrew GCC 9.2.0) 9.2.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

調査

test.cpp

#include <bits/stdc++.h>
using namespace std;

int main() {
    int a[] = {3, 5, 2, 1, 9, 8};

    // 降順
    priority_queue<int> pq;

    // 昇順
    priority_queue<int, vector<int>, greater<int>> pq2;

    for (int i = 0; i < 6; i++) {
        pq.push(a[i]);
        pq2.push(a[i]);
    }

    while (!pq.empty()) {
        cout << pq.top() << " ";
        pq.pop();
    }
    cout << endl;


    while (!pq2.empty()) {
        cout << pq2.top() << " ";
        pq2.pop();
    }
    cout << endl;
}

出力結果

9 8 5 3 2 1
1 2 3 5 8 9

参考