c++优先队列使用结构体

#include <iostream>
#include <queue>
#include <string>
using namespace std;

struct package
{
    int id;
    string data;
};

bool operator<(package a, package b) {
    return a.id < b.id;
}    //自定义排序规则

int main() {
    priority_queue<package> tmp;
    tmp.push({3,"a"});
    tmp.push({2,"b"});
    int size = tmp.size();
    while(size--) {
        cout << tmp.top().id << " " << tmp.top().data <<endl;
        tmp.pop();
    }
}

猜你喜欢

转载自blog.csdn.net/chent86/article/details/78986875