当前位置:网站首页>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
边栏推荐
猜你喜欢

Slipper - virtual point, shortest path

【OpenCV】-重映射

一篇文章看懂JS闭包,从执行上下文角度解析有趣的闭包
一个项目的整体测试流程有哪几个阶段?测试方法有哪些?

Use of lombok annotation @RequiredArgsConstructor

持续投入商品研发,叮咚买菜赢在了供应链投入上

Flask Framework Beginner-06-Add, Delete, Modify and Check the Database

字符串的排列

Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment

114. How to find the cause of Fiori Launchpad routing error by single-step debugging
随机推荐
114. How to find the cause of Fiori Launchpad routing error by single-step debugging
【OpenCV】-重映射
How to copy baby from Taobao (or Tmall store) through API interface to Pinduoduo interface code docking tutorial
持续投入商品研发,叮咚买菜赢在了供应链投入上
GNSS【0】- 专题
字符串的排列
贴纸拼词 —— 记忆化搜索 / 状压DP
Web APIs BOM- 操作浏览器:swiper 插件
ASP.NET 获取数据库的数据并写入到excel表格中
nodejs+npm的安装与配置
Slipper —— 虚点,最短路
Promise solves blocking synchronization and turns asynchronous into synchronous
通用的测试用例编写大全(登录测试/web测试等)
Sky map coordinate system to Gaode coordinate system WGS84 to GCJ02
C程序编译和预定义详解
Deng Qinglin, Alibaba Cloud Technical Expert: Best Practices for Disaster Recovery across Availability Zones and Multiple Lives in Different Locations on the Cloud
工程制图复习题(带答案)
[store mall project 01] environment preparation and testing
Parquet encoding
Summary of GNSS Articles