Priority Queue STL

Source: Internet
Author: User

The priority queue is one of the queues, but it can dynamically sort the data in the queue in a custom way (priority of the data) every push and pop operation, and the queue dynamically adjusts 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 we push a bunch of numbers in what order, and eventually 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<int

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 compare symbol overloads default < symbol

1 structnode2 {3FriendBOOL operator<(node n1, node N2)4     {5         returnN1.priority <n2.priority;6     }7     intPriority ;8     intvalue;9};

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

1#include <iostream>2#include <functional>3#include <queue>4 usingNamespace stdnamespace std;5 structnode6 {7FriendBOOL operator<(node n1, node N2)8     {9         returnN1.priority <n2.priority;Ten     } One     intPriority ; A     intvalue; - }; - intMain () the { -     Const intLen =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 (); -     } About     return 0; $}

Priority Queue 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.