- #include <iostream>
- #include <queue>
- using namespace std;
- struct Message{
- char Name[100];
- int Data;
- int Priority;
- bool operator < (const Message &a)const
- {
- return a.Priority < Priority;
- }
- };
- priority_queue<Message> v;
- int main(int argc,char* argv[])
- {
- char command[100];
- Message message ;
- while(scanf("%s",command)!=EOF)
- {
- if(strcmp(command,"GET")==0)
- {
- if(v.size()==0)
- {
- printf("EMPTY QUEUE!");
- }
- else{
- printf("%s,%d",v.top().Name,v.top().Data);
- v.pop();
- }
- }
- else if(strcmp(command,"PUT")==0)
- {
- scanf("%s%d%d",&message.Name,&message.Data,&message.Priority);
- v.push(message);
- }
- }
- return 0;
- }
测试结果为:
- GET
- EMPTY QUEUE!
- PUT msg1 10 5
- PUT msg2 10 4
- GET
- msg2,10
- GET
- msg1,10
- GET
- EMPTY QUEUE!
重载操作符的 定义方法为:
- bool operator < (const Message &a)const
- {
- return a.Priority < Priority;
- }