struct で operator を定義してみる (C++)
C++
Published: 2020-03-14

確認環境

$ 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;

struct edge {
    int cost, hoge1, hoge2;
    bool operator<(const edge& rhs) const {
        return cost > rhs.cost;
    }
};

int main() {
    priority_queue<edge> pq;

    for (int i = 0; i < 5; i++) {
        pq.push({10 + i, 2, 3});
    }

    while (!pq.empty()) {
        edge e = pq.top();
        pq.pop();
        cout << e.cost << " " << e.hoge1 << " " << e.hoge2 << endl;
    }
}

出力結果

10 2 3
11 2 3
12 2 3
13 2 3
14 2 3

参考