当前位置:网站首页>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
边栏推荐
- nodejs+express realizes the access to the database mysql and displays the data on the page
- nodejs installation and environment configuration
- - heavy OpenCV 】 【 mapping
- GraphQL背后处理及执行过程是什么
- 一篇文章看懂JS闭包,从执行上下文角度解析有趣的闭包
- esp32 releases robot battery voltage to ros2 (micro-ros+CoCube)
- idea中diagram使用
- 通用的测试用例编写大全(登录测试/web测试等)
- Promise solves blocking synchronization and turns asynchronous into synchronous
- html select标签赋值数据库查询结果
猜你喜欢
随机推荐
.NET Static Code Weaving - Rougamo Release 1.1.0
Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment
Web APIs BOM- 操作浏览器:swiper 插件
2022年上半年各大厂Android面试题整理及答案解析(持续更新中......)
一个项目的整体测试流程有哪几个阶段?测试方法有哪些?
C程序编译和预定义详解
实例037:排序
FeatureNotFound( bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested:
特征值与特征向量
【日志框架】
OpenCV如何实现Sobel边缘检测
浏览器存储
nodejs安装及环境配置
Deng Qinglin, Alibaba Cloud Technical Expert: Best Practices for Disaster Recovery across Availability Zones and Multiple Lives in Different Locations on the Cloud
Hey, I had another fight with HR in the small group!
工程制图复习题(带答案)
The 600MHz band is here, will it be the new golden band?
Installation and configuration of nodejs+npm
实例040:逆序列表
快速入门EasyX图形编程









