Priority queue of STL

Source: Internet
Author: User

Priority_queue priority queue is a one-way queue with the concept of ownership value. In this queue, all elements are arranged by priority (you can also think of the queue as a priority queue based on the order of the incoming queue-the element that first enters the queue has a higher priority than the element that then enters the queue ). In computer operating systems, priority queues are frequently used for thread scheduling. In the specific implementation of STL, priority_queue uses other containers as the bottom structure, and then adjusts the positions between elements according to the heap processing rules.

Priority_queue function list
Function Description
Structure
Priority_queue <ELEM> C
Create an empty queue.

Data access and increase/decrease
C. Top () Back to queue header data
C. Push (ELEM) Add ELEM data at the end of the queue
C. Pop () Data in the queue header goes out of the queue
Other operations
C. Empty () Determine whether the queue is empty
C. Size ()

Returns the number of data in the queue.

 

We can see that the priority_queue function list is the same as the stack function list.

1. The standard library uses the <operator of the element type by default to determine the priority relationship between them.

priority_queue<int> pq;

2. The smaller the data, the higher the priority.

<pre name="code" class="cpp">priority_queue<int, vector<int>, greater<int> >pq; 

3. The higher the data, the higher the priority. 

priority_queue<int, vector<int>, less<int> >pq; 

4. Custom priority, heavy load <symbol

struct node{    friend bool operator< (node n1, node n2)    {        return n1.priority < n2.priority;    }    int priority;    int value;};
Note: Reload> numbers will be compiled incorrectly, because the standard library uses the element type <operator by default to determine the priority relationship between them.
Detailed control priority code:

struct cmp1{      bool operator ()(int &a,int &b){          return a>b;//最小值优先       }  };  struct cmp2{      bool operator ()(int &a,int &b){          return a<b;//最大值优先       }  };  struct node1{      int u;      bool operator < (const node1 &a) const {         return u>a.u;//最小值优先       }  };  struct node2{  int u;  bool operator < (const node2 &a) const {          return u<a.u;//最大值优先   }  }; priority_queue<int>q1;//采用默认优先级构造队列     priority_queue<int,vector<int>,cmp1>q2;//最小值优先   priority_queue<int,vector<int>,cmp2>q3;//最大值优先   priority_queue<int,vector<int>,greater<int> >q4;//注意“>>”会被认为错误,                                                   //这是右移运算符,所以这里用空格号隔开,最小值优先 priority_queue<int,vector<int>,less<int> >q5;//最大值优先    priority_queue<node1>q6;  //自定义优先级priority_queue<node2>q7;


The following example shows how to use an advanced queue.

#include <queue>#include <list>#include <cstdio>using namespace std;int main(){//优先级队列默认是使用vector作容器。priority_queue<int> a;priority_queue<int, list<int>> b; //可以这样声明,但无法使用int i;//压入数据for (i = 0; i < 10; i++){a.push(i * 2 - 5);//b.push(i); //编译错误}//优先级队列的大小printf("%d\n", a.size());//取优先级队列数据并将数据移出队列while (!a.empty()){printf("%d ", a.top());a.pop();}putchar('\n');return 0;}

The following program is for struct, and the comparison of the data is to reload operator () on the struct ().

The program function is to simulate the queuing process. Each person has a name and a priority. If the priority is the same, the system compares the name. Five people enter the queue, and two people leave the queue and three others enter the queue, at last, all members leave the team in sequence, and the program will output the order of leaving the team.

#include <queue>#include <cstring>#include <cstdio>using namespace std;//结构体struct Node{char szName[20];int  priority;Node(int nri, char *pszName){strcpy(szName, pszName);priority = nri;}};//结构体的比较方法 改写operator()struct NodeCmp{bool operator()(const Node &na, const Node &nb){if (na.priority != nb.priority)return na.priority <= nb.priority;elsereturn strcmp(na.szName, nb.szName) > 0;}};void PrintfNode(Node &na){printf("%s %d\n", na.szName, na.priority);}int main(){//优先级队列默认是使用vector作容器,底层数据结构为堆。priority_queue<Node, vector<Node>, NodeCmp> a;//有5个人进入队列a.push(Node(5, "小谭"));a.push(Node(3, "小刘"));a.push(Node(1, "小涛"));a.push(Node(5, "小王"));//队头的2个人出队PrintfNode(a.top());a.pop();PrintfNode(a.top());a.pop();printf("--------------------\n");//再进入3个人a.push(Node(2, "小白"));a.push(Node(2, "小强"));a.push(Node(3, "小新"));//所有人都依次出队while (!a.empty()){PrintfNode(a.top());a.pop();}return 0;}

Priority queue of STL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.