当前位置:网站首页>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
边栏推荐
猜你喜欢
Flask Framework Beginner-06-Add, Delete, Modify and Check the Database
Vant3 - click on the corresponding name name to jump to the next page corresponding to the location of the name of the TAB bar
【日志框架】
IDEA02:配置SQL Server2019数据库
字符串的排列
Electronics manufacturing enterprise deployment WMS what are the benefits of warehouse management system
Android interview questions and answer analysis of major factories in the first half of 2022 (continuously updated...)
谁说程序员不懂浪漫,表白代码来啦~
Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment
Use nodejs switch version (no need to uninstall and reinstall)
随机推荐
TensoFlow学习记录(二):基础操作
【日志框架】
nodejs install multi-version version switching
lombok注解@RequiredArgsConstructor的使用
KunlunBase 1.0 发布了!
Vant3 - click on the corresponding name name to jump to the next page corresponding to the location of the name of the TAB bar
JS 保姆级贴心,从零教你手写实现一个防抖debounce方法
C # WPF equipment monitoring software (classic) - the next
boot issue
多渠道打包
MongoDB数据接入实践
【虚拟化生态平台】虚拟化平台esxi挂载USB硬盘
计算首屏时间
Promise 解决阻塞式同步,将异步变为同步
即席查询——Presto
vxe-table 从页面批量删除数据 (不动数据库里的数据)
Apache DolphinScheduler新一代分布式工作流任务调度平台实战-中
DDTL:远距离的域迁移学习
特征值与特征向量
静态/动态代理模式