STL Priority Queue (1)

Source: Internet
Author: User

Priority Queue Usage

In the priority queue, elements with high priority are first out of the queue.

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

First use of the priority queue:

is also the most common usage

Priority_queue<int> Qi;

The < operator shows the high precedence of the elements in integers.

Therefore, the output in example 1 is:9 6 5 3 2

Second usage:

In example 1 , what if we want to export the element from small to large?

At this point we can pass in a comparison function, using the functional.h function Object as the comparison function.

Priority_queue<int, Vector<int>, greater<int> >qi2; priority queue from small to large

You can change the greater to less, which is from big to small

Where the first parameter is a container type. The second argument is a comparison function.

Therefore, the output in example 2 is:2 3 5 6 9

Third usage: Custom precedence.

struct node

{

friend bool operator< (node n1, node N2)

{

return n1.priority < n2.priority; "<" arranges ">" for Small to large

}

int priority;

int value;

};

In this structure, value is the values and priority is the precedence.

The priority in the element is compared by customizing the operator< operator.

In Example 3 , the output is:

Priority Value

9 5

8 2

6 1

2 3

1 4

But if the structure is defined as follows:

struct node

{

friend bool Operator> (node n1, node N2)

{

return n1.priority > n2.priority;

}

int priority;

int value;

};

It will compile but (g++ compiler)

Because the standard library uses the < operator of the element type by default to determine the precedence relationship between them.

and the < operator of the custom type has no direct contact with the > operator, so it compiles.

Code Listing

1#include <iostream>2#include <functional>3#include <queue>4 using namespacestd;5 structnode6 {7FriendBOOLoperator<(node n1, node N2)8     {9 returnN1.priority <n2.priority;Ten     } OneintPriority ; Aintvalue; - }; - intMain () the { -ConstintLen =5; -inti; -intA[len] = {3,5,9,6,2}; +//Example 1 -priority_queue<int>Qi; + for(i =0; i < Len; i++) A Qi.push (A[i]); at for(i =0; i < Len; i++) -     { -Cout<<qi.top () <<" "; - Qi.pop (); -     } -cout<<Endl; in//Example 2 -priority_queue<int, vector<int, greater<int> >Qi2; to for(i =0; i < Len; i++) + Qi2.push (A[i]); - for(i =0; i < Len; i++) the     { *Cout<<qi2.top () <<" "; $ Qi2.pop ();Panax Notoginseng     } -cout<<Endl; the //Example 3 +Priority_queue<node>qn; A node B[len]; theb[0].priority =6; b[0].value =1;  +b[1].priority =9; b[1].value =5;  -b[2].priority =2; b[2].value =3;  $b[3].priority =8; b[3].value =2;  $b[4].priority =1; b[4].value =4;  -  - for(i =0; i < Len; i++) the Qn.push (B[i]); -cout<<"Priority Level"<<'\ t'<<"value"<<Endl;Wuyi for(i =0; i < Len; i++) the     { -Cout<<qn.top () .priority<<'\ t'<<qn.top () .value<<Endl; Wu Qn.pop (); -     } Aboutreturn0; $}
View Code

STL Priority Queue (1)

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.