Windows消息队列
消息队列是Windows系统的基础。对于每个进程,系统维护一个消息队列。如果在进程中有特定事件发生,如点击鼠标、文字改变等,系统将把这个消息加到队列当中。同时,如果队列不是空的,这一进程循环地从队列中按照优先级获取消息。请注意优先级值低意味着优先级高。请编辑程序模拟消息队列,将消息加到队列中以及从队列中获取消息。
输入格式:
输入首先给出正整数N(le 10^5≤105),随后N行,每行给出一个指令——GET
或PUT
,分别表示从队列中取出消息或将消息添加到队列中。如果指令是PUT
,后面就有一个消息名称、以及一个正整数表示消息的优先级,此数越小表示优先级越高。消息名称是长度不超过10个字符且不含空格的字符串;题目保证队列中消息的优先级无重复,且输入至少有一个GET
。
输出格式:
对于每个GET
指令,在一行中输出消息队列中优先级最高的消息的名称和参数。如果消息队列中没有消息,输出EMPTY QUEUE!
。对于PUT
指令则没有输出。
输入样例:
9
PUT msg1 5
PUT msg2 4
GET
PUT msg3 2
PUT msg4 4
GET
GET
GET
GET
输出样例:
msg2
msg3
msg4
msg1
EMPTY QUEUE!
#include <iostream> #include<cstdio> #include<cstring> #include<map> #include<set> #include<queue> #include<vector> #include<deque> #include<algorithm> using namespace std; int n; typedef struct cmp { friend bool operator<(cmp a,cmp b) { return a.key>b.key; } char str[15]; int key; } cmpp; //关键的比较函数,写在结构体里面 int main() { scanf("%d",&n); priority_queue<cmpp> Q; //定义的不同写法。和原来priority_queue<node,vector<node>,cmp> Q;不同 for(int i=1;i<=n;i++) { char ch[5]; scanf("%s",&ch); if (ch[0]=='P') { cmpp u; scanf("%s%d",&u.str,&u.key); Q.push(u); } else { if (Q.empty()) printf("EMPTY QUEUE! "); else { cmpp u=Q.top(); printf("%s ",u.str); Q.pop(); } } } return 0; }
//这个是有问题的,把那种 node(int a,int b){x=a; y=b;}函数写入结构体内,这个结构体的名字就无法命名,make_pair不会用。 #include <iostream> #include<cstdio> #include<cstring> using namespace std; struct node { int x,y; node(int a,int b){x=a; y=b;} }; int main() { node a[100]; //错误 return 0; }