当前位置:网站首页>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
边栏推荐
猜你喜欢
Array_Sliding window | leecode brushing notes
5.scrapy中间件&分布式爬虫
nodejs+express realizes the access to the database mysql and displays the data on the page
静态文件快速建站
Electronics manufacturing enterprise deployment WMS what are the benefits of warehouse management system
JS 从零教你手写节流throttle
typescript58 - generic classes
实例038:矩阵对角线之和
企业虚拟偶像产生了实质性的价值效益
nodejs切换版本使用(不需要卸载重装)
随机推荐
GNSS【0】- 专题
GNSS[0]- Topic
【无标题】
FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed
一篇文章看懂JS闭包,从执行上下文角度解析有趣的闭包
Continuing to pour money into commodities research and development, the ding-dong buy vegetables in win into the supply chain
5.scrapy中间件&分布式爬虫
工程制图复习题(带答案)
VR panorama shooting online exhibition hall, 3D panorama brings you an immersive experience
敏捷交付的工程效能治理
Apache DolphinScheduler新一代分布式工作流任务调度平台实战-中
Analysis of usage scenarios of mutex, read-write lock, spin lock, and atomic operation instructions xaddl and cmpxchg
浏览器存储
【虚拟化生态平台】虚拟化平台搭建
nodejs+npm的安装与配置
IDEA02:配置SQL Server2019数据库
《Greenplum构建实时数据仓库实践》简介
网络带宽监控,带宽监控工具哪个好
【store商城项目01】环境准备以及测试
如何通过API接口从淘宝(或天猫店)复制宝贝到拼多多接口代码对接教程