Priority queue (go to finishing)

Source: Internet
Author: User

The queue maintains a set of objects, the objects that enter the queue are placed at the tail, and the next element is taken from the header of the queue.

What's special about priority_queue is that it allows the user to prioritize the elements stored in the queue . instead of placing the new element directly at the end of the queue, the queue is placed in front of the element that is lower than its priority . The standard library uses the < operator by default to determine the precedence relationship between objects , so if you want to use a custom object, you need to overload the < operator . the element with the highest priority is removed from the queue each time. efficiency of the priority queue: Its time complexity is O (Logn). N is the number of elements in the queue, and access takes time.

1) Definition of priority queue

include header files: #include <functional> #include <queue>

You can either use an existing data structure with a default priority, or you can either pass in a custom priority comparison object when you define a priority queue, or use a custom object (data structure), but you must reload the good < operator.

2) Common operations for priority queues

Priority queue supported actions

Q. Empty ()          returns True if the queue is empty, otherwise false

Q.size ()               returns the number of elements in the queue

Q.pop ()               Delete the first element of the team, but does not return its value

Q.top ()               returns the element value with the highest priority, but does not delete the element

Q.push (item)      Insert a new element in a priority-based location

#include <iostream> #include <functional> #include <queue> #include <vector>using namespace std ;//define comparison structure struct cmp1{bool operator () (int &a,int &b) {return a>b;//min precedence}};struct cmp2{bool    Operator () (int &a,int &b) {return a<b;//max precedence}};//custom data structure struct number1{int x;    BOOL operator < (const number1 &a) const {return x>a.x;//min precedence}};struct number2{int x; BOOL operator < (const number2 &a) const {return x<a.x;//max precedence}};int a[]={14,10,56,7,83,22,36,91,3,4 7,72,0};number1 Num1[]={14,10,56,7,83,22,36,91,3,47,72,0};number2 Num2[]={14,10,56,7,83,22,36,91,3,47,72,0};int    Main () {priority_queue<int>que;//constructs queue with default priority priority_queue<int,vector<int>,cmp1>que1;//min first priority_queue<int,vector<int>,cmp2>que2;//Maximum Priority Priority_queue<int,vector<int>,greater <int> >que3;// note ">>" will be considered error  , priority_queue<int,vector<int>,less<int> >que4;////Maximum priority priority_queue<number1>que5;  Min-Priority Queue priority_queue<number2>que6;    Maximum priority queue int i;        for (i=0;a[i];i++) {Que.push (a[i]);        Que1.push (A[i]);        Que2.push (A[i]);        Que3.push (A[i]);    Que4.push (A[i]);    } for (i=0;num1[i].x;i++) Que5.push (Num1[i]);    for (i=0;num2[i].x;i++) Que6.push (Num2[i]);    printf ("with default precedence:/n (priority_queue<int>que;)/n");    printf ("Queue 0:/n");        while (!que.empty ()) {printf ("%3d", Que.top ());    Que.pop ();    } puts ("");    Puts ("");    printf ("Adopt struct Custom Priority mode one:/n (priority_queue<int,vector<int>,cmp>que;)/n");    printf ("Queue 1:/n");        while (!que1.empty ()) {printf ("%3d", Que1.top ());    Que1.pop ();    } puts ("");    printf ("Queue 2:/n");        while (!que2.empty ()) {printf ("%3d", Que2.top ());    Que2.pop ();    } puts ("");    Puts (""); printf ("With header file/" functional/"defaultPriority:/n (priority_queue<int,vector<int>,greater<int>/less<int> >que;)/n ");    printf ("Queue 3:/n");        while (!que3.empty ()) {printf ("%3d", Que3.top ());    Que3.pop ();    } puts ("");    printf ("Queue 4:/n");        while (!que4.empty ()) {printf ("%3d", Que4.top ());    Que4.pop ();    } puts ("");    Puts ("");    printf ("Adopt struct Custom Priority Mode II:/N (priority_queue<number>que)/n");    printf ("Queue 5:/n");        while (!que5.empty ()) {printf ("%3d", Que5.top ());    Que5.pop ();    } puts ("");    printf ("Queue 6:/n");        while (!que6.empty ()) {printf ("%3d", Que6.top ());    Que6.pop ();    } puts (""); return 0;} /* Run Result: with default precedence: (priority_queue<int>que;) Queue 0:83 72 56 47 36 22 14 10 7 3 Adopt structure Custom Priority Method one: (Priority_queue<int ,vector<int>,cmp>que;) Queue 1:7 91Queue 2:83 72 56 47 36 22 14 10 7 3 with header file "functional" Within the defined priority: (priority_queue<int,vector<int>,greater<int>/less<int> &GT;que;) Queue 3:7 91Queue 4:83 72 56 47 36 22 14 10 7 3 Adopt structure custom Priority Mode two: (priority_queue<number& Gt;que) Queue 5:7 91Queue 6:83 72 56 47 36 22 14 10 7 3*/

  

Priority queue (go to finishing)

Related Article

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.