优先队列的基本运用……
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
#include <cstdio> #include <cstring> #include <queue> using
namespace std;
struct
Message
{ char
Name[100];
int
Data,Priority,index;
friend
bool operator <( const
Message &a, const
Message &b){
if (a.Priority!=b.Priority) return
a.Priority>b.Priority;
return
a.index>b.index;
}
}; priority_queue v; int
main(){
char
command[100];
Message message;
int
k=0;
while ( scanf ( "%s" ,command)!=EOF){
if ( strcmp (command, "GET" )==0){
if (v.size()==0) printf ( "EMPTY QUEUE!\n" );
else {
printf ( "%s %d\n" ,v.top().Name,v.top().Data);
v.pop();
}
}
else
if ( strcmp (command, "PUT" )==0){
scanf ( "%s%d%d" ,message.Name,&message.Data,&message.Priority);
message.index=++k;
v.push(message);
}
}
return
0;
} |