消息隊列是Windows系統的基礎。對于每個程序,系統維護一個消息隊列。如果在程序中有特定事件發生,如點選滑鼠、文字改變等,系統将把這個消息加到隊列當中。同時,如果隊列不是空的,這一程序循環地從隊列中按照優先級擷取消息。請注意優先級值低意味着優先級高。請編輯程式模拟消息隊列,将消息加到隊列中以及從隊列中擷取消息。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
using namespace std;
const int maxn = 1e5 + 10;
struct Data {
int x, id;
Data(int xx, int i) : x(xx), id(i) {}
bool operator < (const Data &temp) const {
return x > temp.x;
}
};
char s[20], str[maxn][20];
int main() {
int n, x, cnt = 0;
priority_queue<Data> q;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%s", s);
if (s[0] == 'P') {
scanf("%s%d", str[++cnt], &x);
q.push(Data(x, cnt));
}
else {
if (q.empty()) printf("EMPTY QUEUE!\n");
else {
Data d = q.top(); q.pop();
printf("%s\n", str[d.id]);
}
}
}
return 0;
}