Priority_queue Priority Queue/c++

Source: Internet
Author: User

Priority_queue Priority Queue/c++ Overview

Priority_queue is a queue of weighted values that allows elements to be added at the bottom and removed from the top.
Priority_queue with the concept of the right to value, the highest weight, ranked in the front.
By default, the priority_queue is done using a max-heap, which is a complete binary tree with a vector representation.

Defined

Since the priority_queue is completely based on the bottom container, plus the heap processing rules, its implementation is straightforward. By default, vectors are the bottom container.
Priority_queue all elements, in and out have a certain rule, only the top element of the queue (the highest weight), the opportunity to be used by the outside world. Priority_queue does not provide traversal functionality, nor does it provide iterators.
The bottom is used: Make_heap,push_heap,pop_heap (three are all generic algorithms)

Push_heap: First Use the push_back () of the underlying container to push the new element to the end and then rearrange the heap.
Pop_heap: Takes an element out of the heap. Instead of actually ejecting the element, it re-pop_back the heap, and then gets the ejected element with the bottom-level container's ().

template<    class T,    class Container = std::vector<T>,    class Compare = std::less<typename Container::value_type>> class priority_queue;
  • T-the type of the stored elements. The behavior is undefined if T are not the same type as Container::value_type. (Since c++17)
  • Container-the type of the underlying Container to the store the elements. The container must satisfy the requirements of Sequencecontainer, and its iterators, must satisfy the requirements of Rando Maccessiterator. Additionally, it must provide the following functions with the usual semantics:
    Front ()
    Push_back ()
    Pop_back ()
    The standard containers std::vector and std::d eque satisfy these requirements.
    The bottom layer is only vector and deque implementations
  • Compare-a Compare type providing A strict weak ordering.
    Std::greater
Operation
Common Functions function
Top Take the team head element
Empty Determines whether the priority queue is empty
Size Number of elements in the priority queue
Push Add an element to the priority queue
Pop Popup Team Header Element (return value is void)
Example
  #include <functional> #include <queue> #include <vector> #include <iostream> template<typename t> void Print_queue (t& q) {while (!q.empty ()) {Std::cout <<        Q.top () << "";    Q.pop (); } std::cout << ' \ n ';}     int main () {std::p riority_queue<int> q;     for (int n: {1,8,5,6,3,4,0,9,7,2}) Q.push (n);     Print_queue (q);     std::p riority_queue<int, Std::vector<int>, std::greater<int> > Q2;     for (int n: {1,8,5,6,3,4,0,9,7,2}) Q2.push (n);     Print_queue (Q2);    Using Lambda to compare elements.    Auto CMP = [] (int left, int. right) {return (left ^ 1) < (right ^ 1);};     std::p riority_queue<int, Std::vector<int>, Decltype (CMP) > Q3 (CMP);     for (int n: {1,8,5,6,3,4,0,9,7,2}) Q3.push (n); Print_queue (Q3); }

Output
9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9
8 9 6 7 4 5 2 3 0 1

http://www.frankyang.cn/2017/08/31/priorityqueue/

Priority_queue Priority Queue/c++

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.