C + + STL Priority queues__c++

Source: Internet
Author: User

Priority Queue (Priority queues): As the name suggests, a queue with priority . It is a kind of ADT, and the thought of the queue is similar-queue, the data structure of the queues can not cut in line, can not reverse the queue order, and in the priority queue, the first out of the queue elements are not advanced queue elements, but a high priority element, The default priority is the number of large numbers high priority. Of course the user is a customizable type , so you must define a priority for the element. Because the team element is not the most advanced team element, then the team's method has the queue front () becomes top (). -Define

namespace std{
    template <typename T,
              typename Container = Vector<t>,
              typename Compare = less< TypeName container::value_type> >
             class priority_queue;
}

Note Required header file:< queue >

priority_queue<int>pq1;     
or
priority_queue<int,deque<int> >pq3;
-Custom Type
Priority_queue<int,vector<int>,greater<int> >pq2;//priority is defined as the number of small priority large
-Insert Element

push ()

int a[10]={2,3,4,1,8,6,5,7,9,5};
for (int i=0;i<10;i++)
{
    pq1.push (a[i]);
}
-Delete Element

pop ()

Pq1.pop ()//IE Delete team head element, out team
-Take the team head element

Top ()

int e = Pq1.top ();//Assign the value of the team head element to E
-Capacity

sizeof ()

int size = Pq1.size ();
-Instance

Code:

#include <cstdio> #include <queue> #include <algorithm> using namespace std;                          int main () {priority_queue<int>pq1;
    The default is descending int a[10]={2,3,4,1,8,6,5,7,9,5}; 
    for (int i=0;i<10;i++) {Pq1.push (a[i]);
    while (!pq1.empty ()) {printf ("%d", pq1.top ());//Print out 9 8 7 6 5 5 4 3 2 1 pq1.pop ();

    printf ("\ n");
    Priority_queue<int,vector<int>,greater<int> >pq2;//can be rewritten as ascending int b[10]={2,3,4,1,8,6,5,7,9,5};
    for (int i=0;i<10;i++) {Pq2.push (b[i]);
    while (!pq2.empty ()) {printf ("%d", pq2.top ());//Print out 1 2 3 4 5 5 6 7 8 9 pq2.pop ();
    printf ("\ n");
    Priority_queue<int,deque<int> >pq3;
    int c[10]={2,3,4,1,8,6,5,7,9,5};
    for (int i=0;i<10;i++) {Pq3.push (c[i]);
    int size = Pq3.size ();        printf ("%d\n", size); Output a while (!pq3.empty ()) {printf ("%d", pq3.top ());/Print out 9 87 6 5 5 4 3 2 1 pq3.pop ();
return 0; }

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.