Queue-queue details

1. The definition of the queue template class is in the header file.

The queue is very similar to the stack template. The queue template also needs to define two template parameters, one is the element type and the other is the container type. The element type is necessary, the container type is optional, and the default is the dqueue type.
The sample code for defining a queue object is as follows:
queueq1;
queueq2;
The basic operations of queue are:
1. Enqueue: such as q.push(x): connect the x element to the end of the queue;
2. Dequeue: such as q.pop( ) Pop the first element of the queue, and will not return the value of the element;
3, access the first element of the queue: such as q.front()
4, access the tail element, such as q.back();
5, access the element in the queue The number of elements, such as q.size();

Here is the best entry-level queue question
UVa-540 Team Queue
with AC code attached:

#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;
} 

2. Priority queue
In the <queue>header file, a very useful template class priority_queue (priority queue) is also defined. The difference between the priority queue and the queue is that the priority queue is not dequeued in the order of entry, but according to the priority of the elements in the queue. Dequeue the right order (the default is the larger one first, you can also specify your own priority order by specifying the operator).
The basic operations of the priority queue are:
1. Enqueue: such as q.push(x): connect the x element to the end of the queue;
2. Dequeue: such as q.pop() pops the first element of the queue, not Will return the value of the element;
3, access the first element of the team: such as q.top()

The priority_queue template class has three template parameters, element type, container type, and comparison operator. The latter two can be omitted. The default container is vector, and the default operator is less, that is, the small ones go forward and the large ones go backward (the elements at the end of the sequence are dequeued when the queue is dequeued). The sample code to define the priority_queue object is as follows:

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

The basic operations of priority_queue are the same as those of queue. When beginners use priority_queue, the most difficult thing may be how to define the comparison operator. If it is a basic data type, or a class that has defined a comparison operator, you can directly use the less operator and greater operator of the STL - the default is to use the less operator, that is, the small ones go forward, and the big ones go first. If you want to define your own comparison operator, there are many ways, here is one of them: overloading the comparison operator. The priority queue attempts to substitute two elements x and y into the comparison operator (for the less operator, call x less than y, for the greater operator, call x greater than y), if the result is true, then x is sorted before y, and y will be Dequeue before x, otherwise, y will be queued before x, and x will be dequeued first.

If you want to define a priority queue yourself. You can define a structure cmp, overload the "()" operator to make it look like a function, and then priority_queue<int ,vector<int>,cmp>pqdefine it in the way. Here is the code defined by cmp:


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

Priority Queue Primer:
UVa-136

AC code:

#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;
} 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325817424&siteId=291194637