欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

在STL中,如何对struct进行sort和优先队列的重载操作探索

最编程 2024-07-30 07:29:53
...

默认的sort和priority一般对数据进行排序的,不能对结构体进行排序,如果非要排序就要使用结构体的重载。
结构体的重载形式:

struct people 
{
int age;
int weight;
bool friend operator<(people x)
{
return weight<x.weight;//若<改为>则顺序改变。
}
}

默认是大顶堆
对于优先队列的定义:
priority_queue<int,vector,greater>
为大根堆;
priority_queue<int,vector,less>
为小根堆

推荐阅读