当前位置:网站首页>Priority_queue element as a pointer, the overloaded operators
Priority_queue element as a pointer, the overloaded operators
2022-08-04 01:40:00 【The Prodigal Son of Xiao Guo】
When using priority_queue to construct the maximum and minimum heap, when it is found that the element in priority_queue is a pointer, the std::greater/std::less function cannot call the overloaded operator of the custom data, and the sorting result is calculated according to the size of the pointer address, causing the max-min heap to fail.
#include #include #include void log(const char* str){std::cout << str;}void log(const int v){std::cout << "" << +v << " ";}struct Stuent{int score = 0;bool operator >(const Stuent& right) const{return this->score > right.score;}};int main(){//construct min heapstd::priority_queue, std::greater> minHeap;for (auto i : { 1,3,5,9,8,6,2,7,4,0 }){Stuent* stu=new Stuent;stu->score = i * 10;minHeap.push(stu);}log("Minimum heap:\n");while (!minHeap.empty()){auto stu = minHeap.top();log(stu->score);printf("%lld\n", (uint64_t)stu);minHeap.pop();delete stu;stu = nullptr;}getchar();return 0;} The minimum heap that fails, you can see that it is sorted by pointer address.

How to solve it?
Implement the comparison function:
constexpr bool operator()(const _Ty& _Left, const _Ty& _Right) const {return _Left > _Right;}The test code is as follows:
#include #include #include void log(const char* str){std::cout << str;}void log(const int v){std::cout << "" << +v << " ";}struct Stuent{int score = 0;bool operator >(const Stuent& right) const{return this->score > right.score;}};struct StuentCmp{bool operator()(const Stuent* left, const Stuent* right) const{return left->score > right->score;}};int main(){//construct min heapstd::priority_queue, StuentCmp> minHeap;for (auto i : { 1,3,5,9,8,6,2,7,4,0 }){Stuent* stu=new Stuent;stu->score = i * 10;minHeap.push(stu);}log("Minimum heap:\n");while (!minHeap.empty()){auto stu = minHeap.top();log(stu->score);minHeap.pop();delete stu;stu = nullptr;}getchar();return 0;} 
References: https://en.cppreference.com/w/cpp/utility/functional/greater
边栏推荐
- 5. Scrapy middleware & distributed crawler
- nodejs installation and environment configuration
- 实例041:类的方法与变量
- Slipper - virtual point, shortest path
- VR panorama shooting online exhibition hall, 3D panorama brings you an immersive experience
- Slipper —— 虚点,最短路
- Google Earth Engine - Calculates the effective width of rivers using publicly available river data
- 简单排序(暑假每日一题 14)
- MongoDB数据接入实践
- 特征值与特征向量
猜你喜欢

Vant3 - click on the corresponding name name to jump to the next page corresponding to the location of the name of the TAB bar

Deng Qinglin, Alibaba Cloud Technical Expert: Best Practices for Disaster Recovery across Availability Zones and Multiple Lives in Different Locations on the Cloud

优秀的测试/开发程序员,是怎样修炼的?步步为营地去执行......

【store商城项目01】环境准备以及测试

redis中常见的问题(缓存穿透,缓存雪崩,缓存击穿,redis淘汰策略)
![[store mall project 01] environment preparation and testing](/img/78/415b18a26fdc9e6f59b59ba0a00c4f.png)
[store mall project 01] environment preparation and testing

Electronics manufacturing enterprise deployment WMS what are the benefits of warehouse management system

谁说程序员不懂浪漫,表白代码来啦~

Web APIs BOM- 操作浏览器:swiper 插件

静态文件快速建站
随机推荐
Deng Qinglin, Alibaba Cloud Technical Expert: Best Practices for Disaster Recovery across Availability Zones and Multiple Lives in Different Locations on the Cloud
贪吃蛇游戏Bug解析及功能扩展
5. Scrapy middleware & distributed crawler
实例040:逆序列表
114. How to find the cause of Fiori Launchpad routing error by single-step debugging
LeetCode third topic (the Longest Substring Without Repeating Characters) trilogy # 3: two optimization
持续投入商品研发,叮咚买菜赢在了供应链投入上
JS 从零教你手写节流throttle
安全至上:落地DevSecOps最佳实践你不得不知道的工具
Parquet encoding
Apache DolphinScheduler新一代分布式工作流任务调度平台实战-中
KunlunBase 1.0 is released!
nodejs+express realizes the access to the database mysql and displays the data on the page
简单排序(暑假每日一题 14)
【虚拟化生态平台】虚拟化平台esxi挂载USB硬盘
工程制图平面投影练习
静态文件快速建站
LDO investigation
Vant3 - click on the corresponding name name to jump to the next page corresponding to the location of the name of the TAB bar
HBuilderX的下载安装和创建/运行项目