The use of the "turn" priority_queue

Source: Internet
Author: User

Http://www.cnblogs.com/flyoung2008/articles/2136485.html

Priority_queue calls STL's Make_heap (), Pop_heap (), PUSH_HEAP () algorithm implementations, which is another form of heap. Write a priority_queue that is similar to the Priority_queue usage in the real STL using the STL heap algorithm to deepen the understanding of priority_queue.

#include <iostream>#include <algorithm>#include <vector>usingnamespacestd;classpriority_queue{    private:        vector<int> data;            public:        voidpush( int t ){             data.push_back(t);             push_heap( data.begin(), data.end());         }                voidpop(){            pop_heap( data.begin(), data.end() );            data.pop_back();        }                inttop() { returndata.front(); }        intsize() { returndata.size(); }        boolempty() { returndata.empty(); }};intmain(){    priority_queue test;    test.push( 3 );    test.push( 5 );    test.push( 2 );    test.push( 4 );        while( !test.empty() ){        cout << test.top() << endl;        test.pop(); }            return0;}
The priority_queue in STL is similar in that it only adds templates and related iterators.

Priority_queue is relatively simple to use for basic types. His template declaration has three parameters:
Priority_queue<type, Container, functional>
Where type is the data type, Container is the container that holds the data, and functional is the element comparison method.
Container must be an array-implemented container, such as a vector, deque but not a list.
In STL, vectors are used by default. The comparison method is used by default with operator<, so if you put the next two parameters by default,
The priority queue is the big top pile, the team head element is the largest.
#include <iostream>#include <queue>usingnamespacestd;intmain(){    priority_queue<int> q;        for( inti= 0; i< 10; ++i ) q.push( rand() );    while( !q.empty() ){        cout << q.top() << endl;        q.pop();    }        getchar();    return0;}
If you want to use a small top heap, you generally have to take the template three parameters are taken in.
The STL defines a functor Greater<&gt, and for a basic type you can declare a small top heap with this functor
#include <iostream>#include <queue>usingnamespacestd;intmain(){    priority_queue<int, vector<int>, greater<int> > q;         for( inti= 0; i< 10; ++i ) q.push( rand() );    while( !q.empty() ){        cout << q.top() << endl;        q.pop();    }        getchar();    return0;}

For custom types, you must overload the operator< yourself or write your own copy function
#include <iostream>#include <queue>usingnamespace std;structNode{    intx, y;    Node( inta= 0, intb= 0 ):        x(a), y(b) {}}; booloperator<( Node a, Node b ){    if( a.x== b.x ) returna.y> b.y;    returna.x> b.x; }intmain(){    priority_queue<Node> q;        for( inti= 0; i< 10; ++i )    q.push( Node( rand(), rand() ) );        while( !q.empty() ){        cout << q.top().x << ‘ ‘<< q.top().y << endl;        q.pop();    }        getchar();    return0;}

After the custom type overloads the operator<, the object can be declared with only one template parameter.
But it can't be declared like a basic type at this point
Priority_queue<node, Vector<node>, greater<node> >;
The reason is that greater<node> is not defined, and if you want to define it this way, you can do so as follows:
#include <iostream>#include <queue>usingnamespacestd;struct Node{    intx, y;    Node( inta= 0, intb= 0 ):        x(a), y(b) {}};structcmp{    booloperator() ( Node a, Node b ){        if( a.x== b.x ) returna.y> b.y;                returna.x> b.x; }}; intmain(){    priority_queue<Node, vector<Node>, cmp> q;        for( inti= 0; i< 10; ++i )    q.push( Node( rand(), rand() ) );        while( !q.empty() ){        cout << q.top().x << ‘ ‘<< q.top().y << endl;        q.pop();    }         getchar();    return0;} //以上代码实现的是一个小顶堆

Reprint: http://blog.chinaunix.net/space.php?uid=533684&do=blog&cuid=2615612

PS: If heavy duty operator > can be used directly priority_queue<node,vector<node>,greater<node>>

The use of the "turn" priority_queue

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.