一:stack
#include<iostream> #include<stack> using namespace std; int main() { stack<int>s; s.push(3); cout<<"长度"<<s.size()<<endl;//1 cout<<s.top()<<endl;//3 s.push(4); cout<<s.top()<<endl;//4 s.pop(); cout<<s.top()<<endl;//3 if(s.empty())//0 { cout<<"1"<<endl; } else cout<<"0"<<endl; }
二:queue
#include<iostream> #include<queue> using namespace std; int main() { queue<int>s; for(int i=0;i<10;i++) { s.push(i); } cout<<s.size()<<endl;//10 cout<<s.front()<<endl;//0 cout<<s.back()<<endl;//9 while(!s.empty()) { cout<<s.front()<<endl; s.pop(); } return 0; }
注意一下stack的top()是栈顶元素,queue的front()是队头,back()是最后一个。
三:priority_queue
注意它只有top和push()的操作和stack()来比较进行理解
#include<bits/stdc++.h> using namespace std; struct node{ int num; friend bool operator < (node a,node b) { return a.num>b.num;//从小到大排序 } }; int main() { priority_queue<int> s;// = priority_queue<int,vector<int>,less<int> >s; for(int i=0;i<=10;i++) s.push(i); while(!s.empty()) { cout<<s.top()<<" ";//10 9 8 7 6 5 4 3 2 1 0 s.pop(); } cout<<endl; priority_queue<int,vector<int>,greater<int> >q;//表示数值小的优先级大 for(int i=0;i<=10;i++) q.push(i); while(!q.empty()) { cout<<q.top()<<" ";//0 1 2 3 4 5 6 7 8 9 10 q.pop(); } /* priority_queue<node> s; node a,b,c; a.num=90; b.num=80; c.num=100; s.push(a); s.push(b); s.push(c); while(!s.empty()) { cout<<s.top().num<<endl; s.pop(); }*/ return 0; }
最后做了一题关于优先队列的题进行学习
Problem Description
看病要排队这个是地球人都知道的常识。
不过经过细心的0068的观察,他发现了医院里排队还是有讲究的。0068所去的医院有三个医生(汗,这么少)同时看病。而看病的人病情有轻重,所以不能根据简单的先来先服务的原则。所以医院对每种病情规定了10种不同的优先级。级别为10的优先权最高,级别为1的优先权最低。医生在看病时,则会在他的队伍里面选择一个优先权最高的人进行诊治。如果遇到两个优先权一样的病人的话,则选择最早来排队的病人。
现在就请你帮助医院模拟这个看病过程。
不过经过细心的0068的观察,他发现了医院里排队还是有讲究的。0068所去的医院有三个医生(汗,这么少)同时看病。而看病的人病情有轻重,所以不能根据简单的先来先服务的原则。所以医院对每种病情规定了10种不同的优先级。级别为10的优先权最高,级别为1的优先权最低。医生在看病时,则会在他的队伍里面选择一个优先权最高的人进行诊治。如果遇到两个优先权一样的病人的话,则选择最早来排队的病人。
现在就请你帮助医院模拟这个看病过程。
Input
输入数据包含多组测试,请处理到文件结束。
每组数据第一行有一个正整数N(0<N<2000)表示发生事件的数目。
接下来有N行分别表示发生的事件。
一共有两种事件:
1:"IN A B",表示有一个拥有优先级B的病人要求医生A诊治。(0<A<=3,0<B<=10)
2:"OUT A",表示医生A进行了一次诊治,诊治完毕后,病人出院。(0<A<=3)
每组数据第一行有一个正整数N(0<N<2000)表示发生事件的数目。
接下来有N行分别表示发生的事件。
一共有两种事件:
1:"IN A B",表示有一个拥有优先级B的病人要求医生A诊治。(0<A<=3,0<B<=10)
2:"OUT A",表示医生A进行了一次诊治,诊治完毕后,病人出院。(0<A<=3)
Output
对于每个"OUT A"事件,请在一行里面输出被诊治人的编号ID。如果该事件时无病人需要诊治,则输出"EMPTY"。
诊治人的编号ID的定义为:在一组测试中,"IN A B"事件发生第K次时,进来的病人ID即为K。从1开始编号。
诊治人的编号ID的定义为:在一组测试中,"IN A B"事件发生第K次时,进来的病人ID即为K。从1开始编号。
Sample Input
7 IN 1 1 IN 1 2 OUT 1 OUT 2 IN 2 1 OUT 2 OUT 1 2 IN 1 1 OUT 1
Sample Output
2 EMPTY 3 1 1
Author
linle
AC代码
#include<bits/stdc++.h> using namespace std; struct node{ int x; int y; friend bool operator < (const node a,const node b) { if(a.y==b.y) return a.x>b.x; else return a.y<b.y; } }s; int main() { char str[5]; int n,a,b; while(~scanf("%d",&n)) { s.x=0; priority_queue<node> que[4]; for(int i=0;i<n;i++) { scanf("%s",str); if(str[0]=='I') { s.x++; scanf("%d%d",&a,&b); s.y=b; que[a].push(s); } else { scanf("%d",&a); if(!que[a].empty()) { printf("%d\n",que[a].top().x); que[a].pop(); } else puts("EMPTY"); } } } return 0; }