Windows Message Queue((hdu1509))基础题
2013-07-28 13:09
134 查看
讲一下优先队列的用法,其实我就想用两种,至于重载运行符<则不是我想了解的。第一种是直接用默认的:priority_queue<int> Q; 这里表示优先队列Q里面的int型的数的优先级是根据数的值的大小。值越大,优先级越高。
第二种是自己写一个比较函数:
priority_queue<type,container,functional> Q;
type表示要进行比较的数据的类型,container则表示保存这个数据的容器,而functional则表示比较函数。
对于比较函数可以这样定义:
struct cmp
{
bool operator()(type a,type b)
{
..........;这里面的比较的方式与sort当中的比较函数写法一样,不过排序方式就有点不用。
}
对于自定义数据类型,要给出大小关系,即重载”<“ 运算符。
struct node{
int x,y,step;
bool operator <(const node &b) const
{
return step>b.step;
}
friend bool operator<(node a,node b)
{
return a.step>b.step;
}
};
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
struct point
{
int x,y,z;
char a[100];
friend bool operator <(point a,point b)
{
if(a.y!=b.y)
return a.y>b.y;
return a.x>b.x;
}
};
int main()
{
int m,n,i,j=1;
priority_queue<point>q;
point next;
char b[100];
while(scanf("%s",b)!=EOF)
{
if(strcmp(b,"GET")==0)
{
if(q.empty())
printf("EMPTY QUEUE!\n");
else
{ next=q.top();
q.pop();
printf("%s %d\n",next.a,next.z);
}
}
else
{
scanf("%s%d%d",next.a,&next.z,&next.y);
next.x=j;
j++;
q.push(next);
}
}
return 0;
}
第二种是自己写一个比较函数:
priority_queue<type,container,functional> Q;
type表示要进行比较的数据的类型,container则表示保存这个数据的容器,而functional则表示比较函数。
对于比较函数可以这样定义:
struct cmp
{
bool operator()(type a,type b)
{
..........;这里面的比较的方式与sort当中的比较函数写法一样,不过排序方式就有点不用。
}
对于自定义数据类型,要给出大小关系,即重载”<“ 运算符。
struct node{
int x,y,step;
bool operator <(const node &b) const
{
return step>b.step;
}
friend bool operator<(node a,node b)
{
return a.step>b.step;
}
};
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
struct point
{
int x,y,z;
char a[100];
friend bool operator <(point a,point b)
{
if(a.y!=b.y)
return a.y>b.y;
return a.x>b.x;
}
};
int main()
{
int m,n,i,j=1;
priority_queue<point>q;
point next;
char b[100];
while(scanf("%s",b)!=EOF)
{
if(strcmp(b,"GET")==0)
{
if(q.empty())
printf("EMPTY QUEUE!\n");
else
{ next=q.top();
q.pop();
printf("%s %d\n",next.a,next.z);
}
}
else
{
scanf("%s%d%d",next.a,&next.z,&next.y);
next.x=j;
j++;
q.push(next);
}
}
return 0;
}
相关文章推荐
- hdu1509(Windows Message Queue) 优先队列
- hdu1509 Windows Message Queue(优先队列水)
- Windows Message Queue--hdu1509
- hdu1509(Windows Message Queue) 优先队列
- HDU1509---Windows Message Queue
- hdu1509 Windows Message Queue(优先队列)
- hdu1509 Windows Message Queue
- Python基础语法以及在windows下如何导入模块
- Go编程基础—windows下Go语言开发环境搭建
- [译]Kinect for Windows SDK开发入门(六):骨骼追踪基础 上
- HDU 1509 Windows Message Queue
- windows, thread ,message queue
- Kinect for Windows SDK开发入门(二):基础知识 上
- Windows编程基础 - 概述http://www.bccn.net/Article/kfyy/cjj/jszl/200412/429_6.html
- C#创建Windows Service(Windows 服务)基础教程
- HDU 1509 Windows Message Queue(优先队列)
- 【java 基础】Windows dos 命令
- 【图文教程】手把手教你如何安装ubuntu(Windows基础上双系统)
- 底层:Windows黑客编程基础
- kali linux攻击机及windowsXp/Metasploit_linux靶机安装及在此基础上的蜜罐系统搭建(下)