Go Use of priority queues in C++stl

Source: Internet
Author: User

Original address

Speaking of the queue, we first think of is first-out, backward-out, then what is the priority queue, in the priority queue, the element is given priority, when the element is accessed, the element with the highest priority is first accessed. That is, the priority queue has the highest first-out behavior characteristics.

Priority queue in header file # include <queue>;

Its declared format is: Priority_queue <int> ans;//declares a prioritized queue of shaping called ans

The basic operations are:

Empty ()//Determine if a queue is null

Pop ()//delete team top element

Push ()//Add an element

Size ()//Returns the number of elements owned in the priority queue

Top ()//return top element of priority queue

The time complexity of the priority queue is O (Logn), n is the number of elements in the queue, and its access takes time.

In the default priority queue, the first-out team with the highest priority. The default int type of priority queue is first out of the queue for the larger number of queues.

However, more often than not, we want to be able to customize its priorities, and here are a few common definitions of priority actions:

#include <iostream>#include<vector>#include<queue>using namespacestd; inttmp[ -]; structCMP1 {BOOL operator()(intXinty) {returnx > y;//high priority for small    }  }; structCMP2 {BOOL operator()(Const intXConst inty) {returnTMP[X] >Tmp[y]; //tmp[] Small priority, because you can change the value of the team outside the team,//so using this method is not a real priority, it is recommended to use struct type.     }  }; structNode {intx, y; FriendBOOL operator<(Node A, Node B) {returna.x > b.x;//structure, the X-Small priority is high    }  }; Priority_queue<int>Q1; Priority_queue<int, vector<int, cmp1>Q2; Priority_queue<int, vector<int, cmp2>Q3; Priority_queue<node>Q4; intMain () {intI,j,k,m,n; intx, y;      Node A;  while(cin>>N) { for(intI=0; i<n;i++) {cin>>a.y>>a.x;          Q4.push (a); } cout<<Endl;  while(!Q4.empty ()) {cout<<q4.top (). Y <<" "<<q4.top () .x<<Endl;          Q4.pop (); } cout<<Endl; }      return 0; }  

Go Use of priority queues in C++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.