您的位置:首页 > 其它

优先队列

2016-01-26 21:49 190 查看
为了明天的讲课 今天一直在看广搜 队列 优先队列 算是自己又多了点知识 学长说看我紧张成这样 好吧他明天讲吧 啊 心一下放松了

今天死磕优先队列了 好吧 来写一下总结

优先队列 我认为就是从一堆数据中挑优先级别高的出队 并不是像对列一样先进先出

优先级的大小是自己定义的 operator是一个重载

比如我现在想让x小的先出队 我就可以这样写

struct node

{

int x,y;

bool operator(const node &n1)const

{

return x>n1.x;

}

}

或者

struct node

{

int x,y;

friend bool operatop(const node n1,const node n2)

{

returen n1.x>n2.x;

}

}

运用优先队列 就是 priority_queue<node>Q;

优先队列 就像一个cmp差不多 可以直接用sort 排序结构体

比如:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<queue>
#define N 220
using namespace std;
struct node
{
int x,y;
bool operator<(node n1)const
{
return x>n1.x;
}
};

int main()
{
node a
;
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d %d",&a[i].x,&a[i].y);
}

sort(a,a+n);

for(int i=0;i<n;i++)
{
printf("%d %d\n",a[i].x,a[i].y);
}
return 0;
}


可能还有好多我不知道的功能 今天就先说到这里 我们下周同一时间再见(哈哈)

以后慢慢发现慢慢更新
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: