队列-queue详解

来源:互联网 发布:网络大数据黑名单查询 编辑:程序博客网 时间:2024/06/10 15:44

一.queue模版类的定义在头文件中。

queue与stack模版非常类似,queue模版也需要定义两个模版参数,一个是元素类型,一个是容器类型,元素类型是必要的,容器类型是可选的,默认为dqueue类型。
定义queue对象的示例代码如下:
queueq1;
queueq2;
queue的基本操作有:
1.入队:如q.push(x):将x元素接到队列的末端;
2.出队:如q.pop() 弹出队列的第一个元素,并不会返回元素的值;
3,访问队首元素:如q.front()
4,访问队尾元素,如q.back();
5,访问队中的元素个数,如q.size();

这里推荐一道最好的入门队列题
UVa-540 Team Queue
附上AC代码:

#include<iostream>#include<string>#include<sstream>#include<set>#include<queue> #include<map>using namespace std;const int MAXT=1000+233;int main(){    int t,_case=0;    while(cin>>t&&t){        ++_case;        cout<<"Scenario #"<<_case<<endl;        map<int, int>team;        for(int i=0;i<t;i++){            int n,x;            cin>>n;            while(n--){                cin>>x;                team[x]=i;            }        }        queue<int>q, q2[MAXT];        while(1){            int x;            string cmd;            cin>>cmd;            if(cmd[0]=='S')                break;            else if(cmd[0]=='D'){                int t=q.front();                cout<<q2[t].front()<<endl;                q2[t].pop();                if(q2[t].empty())                    q.pop();            }else if(cmd[0]=='E'){                cin>>x;                int t=team[x];                if(q2[t].empty())                    q.push(t);                q2[t].push(x);            }        }        cout<<endl;    }    return 0;} 

二.优先队列
<queue>头文件中,还定义了一个非常有用的模版类priority_queue(优先队列),优先队列与队列的差别在于优先队列不是按照入队的顺序出队,而是按照队列中元素的优先权顺序出队(默认为大者优先,也可以通过指定算子来指定自己的优先顺序)。
优先队列的基本操作有:
1.入队:如q.push(x):将x元素接到队列的末端;
2.出队:如q.pop() 弹出队列的第一个元素,并不会返回元素的值;
3,访问队首元素:如q.top()

priority_queue模版类有三个模版参数,元素类型,容器类型,比较算子。其中后两个都可以省略,默认容器为vector,默认算子为less,即小的往前排,大的往后排(出队时序列尾的元素出队)。 定义priority_queue对象的示例代码如下:

priority_queue<int >q1;  priority_queue<pair<int,int> >q2; priority_queue<int,vector<int>,greater<int> >q3;//定义小的先出队

priority_queue的基本操作均与queue相同 初学者在使用priority_queue时,最困难的可能就是如何定义比较算子了。如果是基本数据类型,或已定义了比较运算符的类,可以直接用STL的less算子和greater算子——默认为使用less算子,即小的往前排,大的先出队。如果要定义自己的比较算子,方法有多种,这里介绍其中的一种:重载比较运算符。优先队列试图将两个元素x和y代入比较运算符(对less算子,调用x小于y,对greater算子,调用x大于y),若结果为真,则x排在y前面,y将先于x出队,反之,则将y排在x前面,x将先出队。

如果要自己定义一个优先队列。可以定义一个结构体cmp,重载“()”运算符,使其看上去像一个函数,然后用priority_queue<int ,vector<int>,cmp>pq 的方式定义。下面是cmp定义的代码:

struct cmp{    bool operator()(const int a,const int b)const{        retrun a%10>b%10;//这里是实现一个“个位数大的整数优先级小”的优先队列    }};

优先队列入门题:
UVa-136

AC代码:

#include<iostream>#include<vector>#include<queue>#include<set>#include<algorithm>using namespace std;typedef long long LL;const int coeff[3]={2,3,5};int main(){    priority_queue<LL,vector<LL>,greater<LL> >pq;    set<LL>s;    pq.push(1);    s.insert(1);    for(int i=1;;i++){        LL x=pq.top();pq.pop();        if(i==1500){            cout<<"The 1500'th ugly number is "<<x<<".\n";            break;        }        for(int j=0;j<3;j++){            LL x2=x*coeff[j];            if(!s.count(x2)){                s.insert(x2);                pq.push(x2);            }        }    }    return 0;} 
原创粉丝点击