Priority_queue of the container adapter

Source: Internet
Author: User

Template <class T, class Container = Vector<t>
Class Compare = Less<typename container::value_type> > class priority_queue;

Priority queue
Priority queues was a type of container adaptors, specifically designed such that it first element is always the greatest Of the elements it contains, according to some strict weak ordering criterion.
[Priority queue] is a container adapter that moves the highest-priority elements to the top of the team based on strict weak ordering.
This context was similar to a heap, where elements can being inserted at any moment, and only the Max heap element can be RETR Ieved (the one at the top and the priority queue).
[This context of the priority queue is like a heap, and the element can be inserted into the heap at any moment and can only read the maximum heap element (that is, the element at the top of the priority queue)]
Priority queues is implemented as container adaptors, which is classes that use an encapsulated object of a specific con Tainer class as its underlying container, providing a specific set of member functions to access its elements. Elements was popped from the ' back ' of the specific container, which is known as the top of the ' priority ' queue.
[Container adapter is a class that uses a specific container class object as its inner container, which provides a series of member functions to read its elements. element pops up from the end of the inner container, which pops up from the top of the priority queue]
The underlying container is any of the standard container class templates or some other specifically designed Containe R class. The container shall is accessible through random access iterators and support the following operations:
[The inner container of the priority queue can be a standard container class template or other specially designed container class, but in any case, the intrinsic container should support the random storage iterator and the following operations:]
Empty ()
Size ()
Front ()
Push_back ()
Pop_back ()
The standard container classes vector and deque fulfill these requirements. By default, if no container class was specified for a particular priority_queue class instantiation, the standard container Vector is used.
[Standard container vectors and deque meet these requirements.] The default inner container is vector]
Support of random access iterators are required to keep a heap structure internally at the all times. This is do automatically by the container adaptor by automatically calling the algorithm functions make_heap, push_heap and pop_heap when needed.
[Support for random storage iterators requires that the priority queue must be kept within a heap structure at all times, which is done by the container adapter by automatically calling the algorithm function make_heap, push_heap, Pop_heap

A heap data structure is an array object that can be considered a complete binary tree structure. It is characterized by a parent node whose value is greater than (less than) two child nodes (known as the Big Top heap and the small top heap, respectively). It is often used to manage information during the execution of algorithms, including heap sequencing, priority queuing, and so on.

/*  Construct Priority_queuepriority_queue (const compare& comp = Compare (), const container& CTNR = Container ());p Riority_queue (inputiterator First, Inputiterator last, const compare& comp = Compare (), const container& CTNR = C Ontainer ()); A priority_queue keeps internally a comparing function and a container object as data, which is copies of comp and Ctnr R Espectively. [The priority queue will have a comparison function and a container object data, which are the arguments passed to the constructor in each comp and CTNR] The range version (2), on top, inserts the elements between first and last (before the container was converted into a Heap). [The second method inserts the element between [first, second] into the priority queue, and then transforms the heap (sorted by Make_heap)]compcomparison object to being used to order the heap. [Comp is the comparison object used to sort the heap] This is a function pointer or function object able to perform a strict weak ordering by comparing its-arguments. [Comp can be a function pointer or function object that can perform a strictly weak sort, with two parameters] Compare is the third class template Paramete (by default:less<t>). [Comp's data type is the third template parameter for the priority queue, Less<t>]ctnrcontainer object by default. [Ctnr is aContainer Class object]container is the second class template parameter (the type of the underlying Container for the priority_queue; by D efault:vector<t>). [Ctnr data type is the second template parameter of the priority queue, which is the type of the inner container of the priority queue, which is vector<t&gt by default;]*/#include<iostream>#include<queue>#include<vector>#include<functional>classmycomparison{BOOLreverse; Public: Mycomparison (Const BOOL& revparam =false) {Reverse=Revparam; }    BOOL operator() (Const int& LHS,Const int& RHS)Const    {        if(reverse)return(lhs>RHS); Else return(lhs<RHS); }};intMain () {intMyints[] = {Ten, -, -, -}; std::p riority_queue<int>First ; std::p riority_queue<int> Second (myints, myints+4); typedef std::p riority_queue<int, std::vector<int, mycomparison>Mypq_type;    Mypq_type third; Third.push (Ten); Third.push ( -); Third.push ( -); Third.push ( -); Std::cout<<"Third contains:\n";  while(!Third.empty ()) {Std::cout<<third.top () <<' ';    Third.pop (); } mypq_type Fourth (myints, Myints+4, Mycomparison (true)); Std::cout<<"\nfourth contains:\n";  while(!Fourth.empty ()) {Std::cout<<fourth.top () <<' ';    Fourth.pop (); } std::cout<<'\ n'; System ("Pause"); return 0;}
/*bool Empty () Const;size_type size () const;void push (const value_type& val), void Pop (), const value_type& Top ( ) const;*/#include<iostream>#include<queue>intMain () {std::p riority_queue<int>MYPQ; Mypq.push ( -); Mypq.push ( -); Mypq.push ( -); Mypq.push ( +); Std::cout<<"popping out elements ...";  while(!Mypq.empty ()) {Std::cout<<' '<<Mypq.top ();    Mypq.pop (); } std::cout<<'\ n'; System ("Pause"); return 0;}

Priority_queue of the container adapter

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.