[C ++ STL] Things About C ++ STL-priority_queue (priority queue)

Source: Internet
Author: User

I. Overview

Priority_queue is the queue with the concept of ownership. It allows new elements to be added and old elements to be removed. Call make_heap (), pop_heap (), push_heap () in STL ()AlgorithmImplementation is another form of heap. However, it is a queue, so only elements can be added at the bottom and removed from the top.

Sort: sort by weight, rather than by push. The higher the weight, the lower the weight.

It is allowed to be inserted to the priority queue in any order of size, but the priority queue is retrieved based on the weight.

II. Introduction to heap

1) it uses vector storage and is a Complete Binary Tree.

Heap can be divided into max_heap and min_heap. The former has the maximum weight at the root, and the latter has the minimum weight at the root.

2) Build the heap Process

Elements in the vector are first adjusted to the heap format.

When an element is inserted, the element is placed at the end () of the vector, and the heap is adjusted upwards.

3) Heap algorithm // # include <algorithm>

Make_heap (first, last) // The first heap.

Push_heap (first, last) // Insert the element and adjust it to heap

Pop_heap (first, last) // the pop-up element and adjust it to heap

Sort_heap (first, last) // heap sorting

4) Example

 # include 
  
    # include 
   
     # include 
    
      # include 
     
       using namespace STD; int main (INT argc, char ** argv) {int Ia [9] = {0, 1, 2, 3, 4, 5, 6, 7, 8}; vector 
      
        ivec (IA, IA + 9); make_heap (ivec. begin (), ivec. end (); // # include 
       
         for (INT I = 0; I 
        
       
      
     
    
   
  

3. priority_queue instance

 
# Include <queue> # include <algorithm> # include <iostream> using namespace STD; int main (INT argc, char ** argv) {int Ia [9] = {1, 2, 4, 3, 6, 5, 9, 8, 7}; priority_queue <int> ipq (IA, Ia + 9); cout <"Size:" <ipq. size () <Endl; For (INT I = 0; I <ipq. size (); ++ I) cout <ipq. top () <"; cout <Endl; while (! Ipq. Empty () {cout <ipq. Top () <""; ipq. Pop ();} return 0 ;}

2) Implement priority_queue by yourself

The format of priority_queue in STL is similar to this, but templates and related iterators are added.

 
# Include <iostream> # include <algorithm> # include <vector> using namespace STD; Class priority_queue {PRIVATE: vector <int> data; public: void push (int t) {data. push_back (t); push_heap (data. begin (), Data. end (); // creates a pile of elements in the dynamic array vector} void POP () {pop_heap (data. begin (), Data. end (); // pop up the heap top element and adjust the heap data. pop_back ();} int top () {return data. front ();} int size () {return data. size ();} bool empty (){ Return data. empty () ;}}; int main () {priority_queue test; test. push (3); test. push (5); test. push (2); test. push (4); While (! Test. Empty () {cout <test. Top () <Endl; test. Pop () ;}return 0 ;}

3) in STL, vector is used by default. Operator is used by default for comparison. Therefore, if you set the following two parameters by default, the priority queue is the top heap, with the largest Header element. If you want to use a small top heap, you must include all three parameters in the template. STL defines a function like greater. for basic types, you can use this function to declare a small top heap.

 
# Include <iostream> # include <queue> # include <cstdlib> using namespace STD; int main () {priority_queue <int, vector <int>, greater <int> q; for (INT I = 0; I <10; ++ I) Q. push (RAND (); While (! Q. Empty () {cout <q. Top () <Endl; q. Pop () ;}return 0 ;}

4) for a user-defined type, you must reload the operator yourself <or write the imitation function yourself.

# Include <iostream> # include <queue> # include <cstdlib> using namespace STD; struct node {int X, Y; node (int A = 0, int B = 0 ): X (A), y (B) {}// initialization}; bool operator <(node A, Node B) {if (. X = B. x) return. y> B. y; else return. x> B. X ;}int main () {priority_queue <node> q; For (INT I = 0; I <10; ++ I) Q. push (node (RAND (), Rand (); While (! Q. empty () {cout <q. top (). x <''<q. top (). Y <Endl; q. pop () ;}return 0 ;}

 5) after the operator is reloaded for the custom type, only one template parameter can be included when the object is declared. However, you cannot declare priority_queue <node, vector <node>, greater <node> like the basic type;The reason is that greater <node> is not defined. If you want to use this method to define it, you can use the following method:

 
# Include <iostream> # include <queue> using namespace STD; struct node {int X, Y; node (int A = 0, int B = 0): X (), Y (B) {}}; struct CMP {bool operator () (node A, Node B) {if (. X = B. x) return. y> B. y; return. x> B. X ;}; int main () {priority_queue <node, vector <node>, CMP> q; For (INT I = 0; I <10; ++ I) Q. push (node (RAND (), Rand (); While (! Q. empty () {cout <q. top (). x <''<q. top (). Y <Endl; q. pop () ;}getchar (); Return 0 ;}





Related Article

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.