HDU1509 FLY

Problem Description

Message queue is the basic fundamental of windows system. For each process, the system maintains a message queue. If something happens to this process, such as mouse click, text change, the system will add a message to the queue. Meanwhile, the process will do a loop for getting message from the queue according to the priority value if it is not empty. Note that the less priority value means the higher priority. In this problem, you are asked to simulate the message queue for putting messages to and getting message from the message queue.

Input

There’s only one test case in the input. Each line is a command, “GET” or “PUT”, which means getting message or putting message. If the command is “PUT”, there’re one string means the message name and two integer means the parameter and priority followed by. There will be at most 60000 command. Note that one message can appear twice or more and if two messages have the same priority, the one comes first will be processed first.(i.e., FIFO for the same priority.) Process to the end-of-file.

Output

For each “GET” command, output the command getting from the message queue with the name and parameter in one line. If there’s no message in the queue, output “EMPTY QUEUE!”. There’s no output for “PUT” command.

Sample Input

GET
PUT msg1 10 5
PUT msg2 10 4
GET
GET
GET

Sample Output

EMPTY QUEUE!
msg2 10
msg1 10
EMPTY QUEUE!

.此程序C++不通过,G++通过

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
using namespace std;
struct node{
	string name;
	int mes;
	int priority;
	int num;
	friend bool operator<(node a, node b){
		if(a.priority!=b.priority){
			return a.priority>b.priority;
		}
		return a.num>b.num;
	}
};
int main(){
	priority_queue<node>q;
	node fir, next;
	string str;
	int k=0;
	while(cin>>str){
		if(str=="GET"){
			if(q.empty()){
				cout<<"EMPTY QUEUE!"<<endl;
			}
			else {
				fir=q.top();
				q.pop();
				cout<<fir.name<<" "<<fir.mes<<endl;
			}
		}
		else{
			cin>>next.name>>next.mes>>next.priority;
			next.num=k++;
			q.push(next);
		}
	}
	return 0;
}

程序原链接https://blog.csdn.net/hjd_love_zzt/article/details/9821043?depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1&utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1

1.为什么有friend?
类的友元函数是定义在类外部,但有权访问类的所有私有(private)成员和保护(protected)成员。尽管友元函数的原型有在类的定义中出现过,但是友元函数并不是成员函数。

友元可以是一个函数,该函数被称为友元函数;友元也可以是一个类,该类被称为友元类,在这种情况下,整个类及其所有成员都是友元。

如果要声明函数为一个类的友元,需要在类定义中该函数原型前使用关键字 friend,
我理解为如果结构体内部要调用结构体就要加friend,朋友关系。
友元函数https://www.runoob.com/cplusplus/cpp-friend-functions.html

2.operator?
operator是C++的关键字,它和运算符一起使用,表示一个运算符函数,理解时应将operator=整体上视为一个函数名版。这是C++扩展运算符功能的方法,虽然样子古怪,但也可以理解:一方权面要使运算符的使用方法与其原来一致,另一方面扩展其功能只能通过函数的方式(c++中,“功能”都是由函数实现的)。

3.优先队列
在题中优先级值越小优先级越大。程序中
if(a.priority!=b.priority){
return a.priority>b.priority;
}
return a.num>b.num;
}
随着输入,num值增大。
当priority相同时小的出,不相同时,priority小的出。
所以我认为这是个排序始终把priority大的放在刚入入的地方就是队尾,相同时num值大的放在队尾。反过来说就是优先级值小的在对头先输出。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值