STL priority queue usage

Today, we do question the priority queue used its usage is not very familiar now sort out.

Required libraries

#include<queue>
using namespace std;

 But I have used bits / stdc ++. H ...

definition

priority_queue<Type, Container, Functional>

Type is the type of data such as int char ah like ah

Container is the default container type vector

Functional comparison means, such as a greater <int> less <int>, or a custom comparison function

Specific usage

Basic Usage 1

priority_queue <int> q;

This is defined as the most basic usage does not require the same three parameters passed into just declare a data type can be

Note that the default is descending priority queue row!

Basic Usage 2

// ascending Queue 
The priority_queue < int , Vector < int >, Greater < int >> Q;
 // descending queue 
The priority_queue < int , Vector < int >, less < int >> Q;

Because declared way of comparison, this time must pass three parameters into it

have to be aware of is:

  • It must be a space between the greater <int> and>, or a right-shift operator!
  • is greater in ascending order, that is, from small to large, are not we take it for granted greater is descending! (So ​​there is greater need to remember ascending up up ~ less is descending down down ~ This is more in line with normal cognition, beating remember ~)

Advanced Usage 1 (operator overloading)

Method 1 using friend bool operator

typedef struct node
{
    int num;
    friend bool operator < (const node & a,const node & b)
    {
        return a.num < b.num ;
    }    
}point;

 priority_queue<point>q;
 

This method is overloaded operators in the definition of the structure is completed, the definition of the priority queue does not need to pass the three parameters seem useless in this small example in solving complex problems, however, it requires the use of structure body to design data structures also have to tell the computer, compare the way.

have to be aware of is:

  • Less than sign can only be overloaded
  • For small to large row only need to change the return statement is less than the number is greater than the number you can just turn and sort of!

Bool operator using Method 2

typedef struct node
{
        int num;
        bool operator<(const node&b)const        
        {
            return    num<b.num;
        }
}point;    

priority_queue<point>q;

Like and Methods friend bool operator 1, but the wording is slightly different I do not like the feeling of disorderly ....

Advanced Usage 2 (rewritable functor)

struct node1 
{
    int x;
};

struct node2 
{
    bool operator() (node1 a, node1 b) 
    {
        return a.x < b.x; 
    }
};

priority_queue<node1, vector<node1>, node2> p;

Rewrite functor this up real trouble with it .... we need to declare two structures do not like ....

Compatible Operating

  • top access head elements
  • Whether empty queue is empty
  • Returns the number of elements in the queue size
  • push element is inserted into the tail (and ordering)
  • emplace place a configuration and inserted into the queue element
  • pop pop the head elements
  • swap exchange content

Guess you like

Origin www.cnblogs.com/dyhaohaoxuexi/p/11247820.html