Priority Queue Basic Usage

Source: Internet
Author: User

Http://www.cnblogs.com/void/archive/2012/02/01/2335224.html

Priority Queue Priority_queue Usage

A priority queue is one of the queues, but it can dynamically sort the data in a queue in a way that is customized (priority of the data)

Each push and pop operation, the queue is dynamically adjusted to the way we expect it to be stored.

For example: Our common operation is to sort the data, Priority Queue default is the high priority of the data

So, regardless of the order in which we push a bunch of numbers, we end up in the queue always top out the largest element.

Usage:

Example: Push the element 5,3,2,4,6 to the priority queue and print its output.

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

priority_queue<int> PQ;

The < operator shows the high precedence of the elements in integers.
Therefore, the output in Example 1 is: 6 5 4 3 2

2. The smaller the data, the higher the priority

priority_queue<int, vector<int>, greater<   

which
The second parameter is a container type.
The second argument is a comparison function.
Therefore, the output in Example 2 is: 2 3 4 5 6

3. Custom priority, overloaded comparison symbol

Overloading Default < Symbols

struct node
{
operator< (node n1, node N2)
{
return n1.priority < n2.priority;
}
int priority;
int value;

At this point, you need to customize a priority for each element.

Note: The overload > number compiles an error 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

Code:

1 #include <iostream>
2 #include <functional>
3 #include <queue>
4Using Namespace stdnamespace std;
5struct node
6 {
7 Friendbooloperator< (node n1, node N2)
: 9
9return n1.priority < n2.priority;
10}
11int priority;
12int value;
13};
14int main ()
15 {
16Constint len =5;
17int i;
18int A[len] = {3,5,9,6,2};
19//Example 1
priority_queue<Int> Qi;
21stfor (i =0; i < Len; i++)
Qi.push (A[i]);
23for (i =0; i < Len; i++)
24 {
Cout<<qi.top () <<"";
Qi.pop ();
27}
cout<<endl;
29//Example 2
priority_queue<int, vector<Int>, greater<Int> >qi2;
31for (i =0; i < Len; i++)
Qi2.push (A[i]);
33for (i =0; i < Len; i++)
34 {
Cout<<qi2.top () <<"";
Qi2.pop ();
37}
cout<<endl;
39//Example 3
Priority_queue<node> qn;
+ node B[len];
b[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;
47
48for (i =0; i < Len; i++)
Qn.push (B[i]);
cout<<"Priority level "<< \t ' << ' value ' < <endl;
51 for (i = 0; I < Len i++)
52 {
53 cout<<qn.top () .priority< < ' \t << Qn.top () .value<<endl;
54 Qn.pop ();
55}
56 return 0;
57}

Priority Queue Basic usage

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.