7-7(排序) Windows消息队列(25 分)

7-7(排序) Windows消息队列(25 分)

消息队列是Windows系统的基础。对于每个进程,系统维护一个消息队列。如果在进程中有特定事件发生,如点击鼠标、文字改变等,系统将把这个消息加到队列当中。同时,如果队列不是空的,这一进程循环地从队列中按照优先级获取消息。请注意优先级值低意味着优先级高。请编辑程序模拟消息队列,将消息加到队列中以及从队列中获取消息。


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int n ;
struct News
{
    char str[10];
    int pow;
    bool operator < (const News &temp) const
    {
        return pow> temp.pow;
    }
};

priority_queue< News > pque;
int s[maxn];
int main()
{
//    std::ios::sync_with_stdio(false);
    int n;
    scanf("%d",&n);
    char type[11];
    News news;
    for(int i = 0 ; i < n ; i++)
    {
        scanf("%s",type);
        if(type[0] == 'P')
        {
            scanf("%s",news.str);
            scanf("%d",&news.pow);
            pque.push(news);
        }
        else
        {
            if(pque.empty())
            {
                printf("EMPTY QUEUE!\n");
            }
            else
            {
                news = pque.top();
                pque.pop();
                printf("%s\n",news.str);
            }
        }
    }
}


猜你喜欢

转载自blog.csdn.net/zhaiqiming2010/article/details/78645102