当前位置:网站首页>priority_queue元素为指针时,重载运算符失效
priority_queue元素为指针时,重载运算符失效
2022-08-04 01:36:00 【萧国浪子】
使用priority_queue构造最大最小堆时,发现priority_queue中元素为指针时,std::greater/std::less函数并不能调用到自定义数据的重载运算符,排序结果是根据指针地址大小计算的,从而导致最大最小堆失效。
#include <iostream>
#include <vector>
#include <queue>
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()
{
//构造最小堆
std::priority_queue<Stuent*, std::vector<Stuent*>, std::greater<Stuent*>> 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("最小堆:\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;
}
失效的最小堆,可以看到是按指针地址排序的。
如何解决呢?
实现比较函数:
constexpr bool operator()(const _Ty& _Left, const _Ty& _Right) const {
return _Left > _Right;
}
测试代码如下:
#include <iostream>
#include <vector>
#include <queue>
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()
{
//构造最小堆
std::priority_queue<Stuent*, std::vector<Stuent*>, 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("最小堆:\n");
while (!minHeap.empty())
{
auto stu = minHeap.top();
log(stu->score);
minHeap.pop();
delete stu;
stu = nullptr;
}
getchar();
return 0;
}
参考资料:https://en.cppreference.com/w/cpp/utility/functional/greater
边栏推荐
- 可变字符串
- vxe-table 从页面批量删除数据 (不动数据库里的数据)
- Flask框架初学-05-命令管理Manager及数据库的使用
- Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment
- GNSS【0】- 专题
- 一个注解替换synchronized关键字:分布式场景下实现方法加锁
- How to find the cause of Fiori Launchpad routing errors by single-step debugging
- Flask Framework Beginner-06-Add, Delete, Modify and Check the Database
- 多线程 之 JUC 学习篇章一 创建多线程的步骤
- KunlunBase 1.0 发布了!
猜你喜欢
IDEA02:配置SQL Server2019数据库
【OpenCV】-重映射
如何用C语言代码实现商品管理系统开发
The idea of the diagram
静态文件快速建站
redis中常见的问题(缓存穿透,缓存雪崩,缓存击穿,redis淘汰策略)
工程制图平面投影练习
Android interview questions and answer analysis of major factories in the first half of 2022 (continuously updated...)
一篇文章看懂JS闭包,从执行上下文角度解析有趣的闭包
html select标签赋值数据库查询结果
随机推荐
typescript56 - generic interface
Promise 解决阻塞式同步,将异步变为同步
VR panorama shooting online exhibition hall, 3D panorama brings you an immersive experience
nodejs 安装多版本 版本切换
如何通过API接口从淘宝(或天猫店)复制宝贝到拼多多接口代码对接教程
一篇文章看懂JS闭包,从执行上下文角度解析有趣的闭包
Web APIs BOM- 操作浏览器:swiper 插件
持续投入商品研发,叮咚买菜赢在了供应链投入上
持续投入商品研发,叮咚买菜赢在了供应链投入上
可变字符串
FeatureNotFound( bs4.FeatureNotFound: Couldn‘t find a tree builder with the features you requested:
KunlunBase 1.0 is released!
Flask Framework Beginner-05-Command Management Manager and Database Use
Is there any jdbc link to Youxuan database documentation and examples?
《The Google File System》新说
数组_滑动窗口 | leecode刷题笔记
谁说程序员不懂浪漫,表白代码来啦~
js中常用的几种遍历处理数据的方法梳理
- heavy OpenCV 】 【 mapping
vxe-table 从页面批量删除数据 (不动数据库里的数据)