Priority_queue of C + +

Source: Internet
Author: User

Blog reprinted from: https://www.cnblogs.com/George1994/p/6477198.html

Objective

Never used a priority queue before, brush algorithm problems to begin to understand, so do a summary. What is the use of it? For example, when you need to get to the maximum minimum value element, but do not want to use the largest minimum heap of the native implementation, STL provides you with a simpler library, is priority_queue, its time complexity is only O (Nlogn).

Description

Depending on the priority of the element being read, this priority depends on the sort function you set, and if you do not set, the default collation is to use operator< to form descending order, that is, from large to small arrangement of large top heap, the first natural is the largest element. And if you don't have a container for storing data container, the default is vector.

namespace Std {    template < class T,           class Container = vector<t>,           class Compare = Less <typename container::value_type> >    class priority_queue;}

Priority_queue provides three basic functions, namely:

    1. Top ()
    2. Push ()
    3. Pop ()

Note that the pop does not return an element, and top returns the element at the top of the heap.

STL provides the greater<>,less<> of the functor, simplifying the process of defining the sorting function. It's OK if you want to use your own defined structure instead of the basic data type, but you need to overload the operator in your custom class, such as:

Class student{    int id;    Char name[20];    bool gender;    BOOL operator < (Student &a) const    {        return ID > a.id;}    };
Use

This is a topic for finding the middle value of the input stream, implemented with the largest minimum heap.

Priority_queue<int, Vector<int>, less<int>> maxheap; Store small values, the higher the value, the higher the priority Priority_queue<int, Vector<int> greater<int>> minheap; Store large values, the lower the value, the higher the priority/** * There  is no need to judge all kinds of judgments *  but be sure to pay attention to the order of precedence of minheap and maxheap, to avoid */void addNum3 (int num) {    Minheap.push (num);    Maxheap.push (Minheap.top ());    Minheap.pop ();    Balance    if (minheap.size () < Maxheap.size ()) {        Minheap.push (maxheap.top ());        Maxheap.pop ();    }}    Double FindMedian3 () {    return maxheap.size () = = Minheap.size ()? (double) (Maxheap.top () + minheap.top ())/2.0: (double) minheap.top ()/1.0;}    void Test () {    this->addnum3 (1);    THIS->ADDNUM3 (2);        cout << this->findmedian3 () << Endl;        THIS->ADDNUM2 (3);        cout << this->findmedian3 () << Endl;}

Output results

1.52
The underlying implementation

Obviously, we can see that the underlying implementation of priority_queue is a heap implementation. The C inside is the container you provided yourself container

void push (value_type&& _val)    {    //Insert element at beginning    C.push_back (_std Move (_val));    Push_heap (C.begin (), C.end (), comp);    } Template<class _valty>    void Emplace (_valty&& .... _val)    {    //Insert element at beginning    C.emplace_back (_std forward<_valty> (_val) ...);    Push_heap (C.begin (), C.end (), comp);    } BOOL Empty () const    {    //test if queue is empty    return (C.empty ());    } Size_type size () const    {    //return length of queue    return (C.size ());    } Const_reference Top () const    {    //return highest-priority element    return (C.front ());    } void push (const value_type& _val)    {    //Insert value in priority order    C.push_back (_val);    Push_heap (C.begin (), C.end (), comp);    } void Pop ()    {    //erase highest-priority element    Pop_heap (C.begin (), C.end (), comp);    C.pop_back ();    }

Priority_queue of 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.