Detailed explanation of c + + priority_queue

Posted by washbucket on Tue, 21 Sep 2021 08:30:37 +0200

catalogue

1. Content:

2. Examples:

1) Examples of basic types:

2) For pair comparison, first compare the first element, the first is equal, and the second is equal  

3) For custom types  

3. Common uses:

 

1. Content:

Since it is a queue, the header file #include < queue > must be included first. The difference between it and queue is that we can customize the priority of the data in it, so that the higher priority is in front of the queue and out of the queue first

Priority queue has all the characteristics of queue, including basic operations. On this basis, it adds an internal sort, which is essentially implemented as a heap

 

 

The basic operations are the same as those of the queue:

  • top access queue header element
  • empty queue
  • size returns the number of elements in the queue
  • push insert elements to the end of the queue (and sort)
  • Empty constructs an element in place and inserts it into the queue
  • pop up queue header element
  • swap exchange content

Definition: priority_ queue<Type, Container, Functional>
Type is the data type, and Container is the Container type (Container must be implemented by array, such as vector,deque, etc., but not list. Vector is used by default in STL), and Functional is the method of comparison. These three parameters need to be passed in when user-defined data types need to be used. When basic data types are used, only data types need to be passed in, The default is large top heap.

  • Priority their settings are all numbers by default. The higher the priority, the higher the priority

    The first element of the queue is the one with the largest element in the priority queue

//Ascending queue
priority_queue <int,vector<int>,greater<int> > q;
//Descending queue
priority_queue <int,vector<int>,less<int> >q;

//greater and less are two imitation functions implemented by std (that is, to make the use of a class look like a function. Its implementation is to implement an operator() in the class, and this class has a function like behavior, which is an imitation function class)

//Less < int > indicates that the higher the number, the greater the priority, the larger the root heap, and the higher the priority is at the head of the team, which pops up first
greater<int>Indicates that the smaller the number, the higher the priority, the smaller the root heap

2. Examples:

1) Examples of basic types:

#include<iostream>
#include <queue>
using namespace std;
int main() 
{
    //For the foundation type, the default is large top heap
    priority_queue<int> a; 
    //Equivalent to priority_ queue<int, vector<int>, less<int> > a;
    
  
    priority_queue<int, vector<int>, greater<int> > c;  //This is the small top pile
    priority_queue<string> b;

    for (int i = 0; i < 5; i++) 
    {
        a.push(i);
        c.push(i);
    }
    while (!a.empty()) 
    {
        cout << a.top() << " ";//4 3 2 1 0
        a.pop();
    } 
    cout << endl;

    while (!c.empty()) 
    {
        cout << c.top() << " ";//0 1 2 3 4 
        c.pop();
    }
    cout << endl;

    b.push("abc");
    b.push("abcd");
    b.push("cbd");
    while (!b.empty()) 
    {
        cout << b.top() << " ";//cbd abcd abc
        b.pop();
    } 
    cout << endl;
    return 0;
}

2) For pair comparison, first compare the first element, the first is equal, and the second is equal  

#include <iostream>
#include <queue>
using namespace std;
int main() 
{
    priority_queue<pair<int, int> > a;
    pair<int, int> b(1, 2);
    pair<int, int> c(1, 3);
    pair<int, int> d(2, 5);
    a.push(d);
    a.push(c);
    a.push(b);
    while (!a.empty()) 
    {
        cout << a.top().first << ' ' << a.top().second << '\n';
        a.pop();
    }
}
Operation results:
    2 5
    1 3
    1 2

3) For custom types  

#include <iostream>
#include <queue>
using namespace std;

//Method 1
struct tmp1 //Operator overloading<
{
    int x;
    tmp1(int a) {x = a;}
    bool operator<(const tmp1& a) const
    {
        return x >a.x; //Small top pile
    }
};

//Method 2
struct tmp2 //Rewriting affine function
{
    bool operator() (tmp1 a, tmp1 b) 
    {
        return a.x < b.x; //Large top reactor
    }
};

int main() 
{
    tmp1 a(1);
    tmp1 b(2);
    tmp1 c(3);
    priority_queue<tmp1> d;//The comparison operator has been overloaded
    d.push(b);
    d.push(c);
    d.push(a);
    while (!d.empty()) 
    {
        cout << d.top().x << " ";//3 2 1
        d.pop();
    }
    cout << endl;

    priority_queue<tmp1, vector<tmp1>, tmp2> f;//Note that the functor only writes the class name
    f.push(c);
    f.push(b);
    f.push(a);
    while (!f.empty()) 
    {
        cout << f.top().x << " ";//1 2 3
        f.pop();
    }
}

3. Common uses:

a. Can solve some greedy problems;

b. dijksta algorithm can also be optimized;

 

Topics: C++ STL