#include iostream#include queueusing namespace std;方法1struct tmp1 运算符重载 int x; tmp1int a x = a; bool operatorconst tmp1& a const return x ax; 大顶堆 ;方法2struct tmp2 重写仿函数 bool
方法二使用了自定义的仿函数tmp2来定义priority_queue的比较规则。在tmp2中,重载了()运算符,使得在插入元素时按照tmp1结构体中x的大小进行比较,从而实现大顶堆的效果。
具体来说,tmp2中的重载函数返回的是a.x < b.x,这表示如果a的x小于b的x,则返回true,否则返回false。根据priority_queue的特性,返回true表示a应该排在b之前,也就是说a的优先级更高,因此插入时会按照大顶堆的规则进行排序。
在主函数中,通过priority_queue<tmp1, vector
原文地址: http://www.cveoy.top/t/topic/iisy 著作权归作者所有。请勿转载和采集!