当前位置:网站首页>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
边栏推荐
- Promise solves blocking synchronization and turns asynchronous into synchronous
- Vant3 - click on the corresponding name name to jump to the next page corresponding to the location of the name of the TAB bar
- 实例035:设置输出颜色
- Flask Framework Beginner-06-Add, Delete, Modify and Check the Database
- Summary of GNSS Articles
- 观察者模式
- Installation and configuration of nodejs+npm
- 通用的测试用例编写大全(登录测试/web测试等)
- esp32 releases robot battery voltage to ros2 (micro-ros+CoCube)
- Promise 解决阻塞式同步,将异步变为同步
猜你喜欢
随机推荐
splice随机添加和删除的写法
ASP.NET 获取数据库的数据并写入到excel表格中
优秀的测试/开发程序员,是怎样修炼的?步步为营地去执行......
appium软件自动化测试框架
Apache DolphinScheduler actual combat task scheduling platform - a new generation of distributed workflow
Promise 解决阻塞式同步,将异步变为同步
Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
Slipper - virtual point, shortest path
阿里云技术专家邓青琳:云上跨可用区容灾和异地多活最佳实践
特征值与特征向量
Sky map coordinate system to Gaode coordinate system WGS84 to GCJ02
ThreadLocal
GNSS[0]- Topic
LeetCode third topic (the Longest Substring Without Repeating Characters) trilogy # 3: two optimization
内网穿透-应用
nodejs切换版本使用(不需要卸载重装)
如何用C语言代码实现商品管理系统开发
哎,又跟HR在小群吵了一架!
【虚拟户生态平台】虚拟化平台安装时遇到的坑
GNSS文章汇总