The queue features FIFO. Generally, queues are similar to queuing for shopping. Everyone is very orderly. the first person in the queue buys something first. The priority queue is different. It does not follow the first-in-first-out rule, but is extracted first with the highest priority based on the priority of the elements in the queue. Generally, the priority queue is equivalent to printing in real life. There are a lot of printers in a print shop, and the performance of each machine is different. Some printers print fast, and some printers print slowly. Wait in the queue when these printers print their tasks in succession. If I want to print a file at this time, I choose not the first printer in the queue, but the best performance and the fastest printer.
Key: priority queue, which depends on the priority. Whoever has a higher priority gets the permission first. No queuing order!
Basic operations:
Empty () returns true if the queue is empty
Pop () deletes the peer Element
Push () to add an element
Size () returns the number of elements in the priority queue.
Top () returns the top-to-top element of the priority queue.
In the default priority queue, a queue with a higher priority is displayed first. In the default int type, a large number is displayed first.
Usage:
Header file:
# Include <queue>
Declaration method:
1. Common Methods:
Priority_queue <int> q;
// The elements are displayed in the ascending order of the elements.
2. Custom priority:
Struct CMP
{
Operator bool () (int x, int y)
{
Return x> Y; // a smaller value of X has a higher priority.
// Other methods can also be written, such as return P [x]> P [y]; indicating that P [I] has a low priority.
}
};
Priority_queue <int, vector <int>, CMP> q; // define a method
// The second parameter indicates the container type. The third parameter is the comparison function.
3. struct declaration method:
Struct Node
{
Int X, Y;
Friend bool operator <(node A, Node B)
{
Return A. x> B. X; // In the struct, the priority of X is higher.
}
};
Priority_queue <node> q; // define a method
// In this structure, Y is the value, and X is the priority.
// Compare the priority of an element using the custom operator <operator.
// When "<" is reloaded, it is best not to reload ">". Compilation Error Examples may occur:
[CPP]View plaincopy
- # Include "iostream"
- # Include "vector"
- # Include "queue"
- Using namespace STD;
- Int C [100];
- Struct CMP1
- {
- Bool operator () (int x, int y)
- {
- Return x> Y; // a small value with a high priority
- }
- };
- Struct cmp2
- {
- Bool operator () (const int X, const int y)
- {
- Return C [x]> C [y];
- // C [x] has a high priority because the value in the team can be changed externally,
- // Therefore, this method does not have a real priority. The struct type is recommended.
- }
- };
- Struct Node
- {
- Int X, Y;
- Friend bool operator <(node A, Node B)
- {
- Return A. x> B. X; // In the struct, the priority of X is higher.
- }
- };
- Priority_queue <int> q1;
- Priority_queue <int, vector <int>, CMP1> Q2;
- Priority_queue <int, vector <int>, cmp2> Q3;
- Priority_queue <node> Q4;
- Queue <int> qq1;
- Queue <node> qq2;
- Int main ()
- {
- Int I, J, K, M, N;
- Int X, Y;
- Node;
- While (CIN> N)
- {
- For (I = 0; I <n; I ++)
- {
- Cin> A. Y> A. X;
- Q4.push ();
- }
- Cout <Endl;
- While (! Q4.empty ())
- {
- Cout <q4.top (). Y <"" <q4.top (). x <Endl;
- Q4.pop ();
- }
- // Cout <Endl;
- }
- Return 0;
- }
Conversion of priority queue in STL