当前位置:网站首页>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
边栏推荐
- Vant3 - click on the corresponding name name to jump to the next page corresponding to the location of the name of the TAB bar
- Web APIs BOM- 操作浏览器:swiper 插件
- Analysis of usage scenarios of mutex, read-write lock, spin lock, and atomic operation instructions xaddl and cmpxchg
- boot issue
- Slipper —— 虚点,最短路
- Flink jdbc connector 源码改造sink之 clickhouse多节点轮询写与性能分析
- 【日志框架】
- Promise 解决阻塞式同步,将异步变为同步
- splice随机添加和删除的写法
- 实例040:逆序列表
猜你喜欢

typescript48 - type compatibility between functions

实例040:逆序列表

114. How to find the cause of Fiori Launchpad routing error by single-step debugging

Use of lombok annotation @RequiredArgsConstructor

GraphQL背后处理及执行过程是什么

appium软件自动化测试框架

OpenCV如何实现Sobel边缘检测

Tanabata festival coming, VR panoramic look god assists for you

工程制图平面投影练习

Deng Qinglin, Alibaba Cloud Technical Expert: Best Practices for Disaster Recovery across Availability Zones and Multiple Lives in Different Locations on the Cloud
随机推荐
【QT小记】QT中信号和槽的基本使用
FeatureNotFound( bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested:
Promise 解决阻塞式同步,将异步变为同步
114. How to find the cause of Fiori Launchpad routing error by single-step debugging
flask框架初学-06-对数据库的增删改查
在Activity中获取另一个XML文件的控件
Use nodejs switch version (no need to uninstall and reinstall)
C 学生管理系统_添加学生
Hey, I had another fight with HR in the small group!
jmeter distributed stress test
boot issue
【日志框架】
特征值与特征向量
一篇文章看懂JS闭包,从执行上下文角度解析有趣的闭包
LDO investigation
Google Earth Engine - Calculates the effective width of rivers using publicly available river data
Analysis: What makes the Nomad Bridge hack unique
JS 从零教你手写节流throttle
Continuing to pour money into commodities research and development, the ding-dong buy vegetables in win into the supply chain
Is there any jdbc link to Youxuan database documentation and examples?