Priority Queue Priority_queue in STL

Source: Internet
Author: User

Priority_queue (queue similar) is completely based on the bottom container, plus the implementation principle of the binary heap (Dagen or small Gan), so its implementation is very simple, by default priority_queue vector as the bottom container. In addition, the default comparison rule for priority_queue is less:

class Compare = Less<typename sequence::value_type>

Less corresponds to implementing the priority queue according to Dagen, and of course the comparison rule can be set to greater, which is the priority queue implemented by Keng Gen.

Priority_queue (queue) completes all its work with the bottom container, which has the property of "modifying something interface to form another style", called an adapter (adapter). Therefore, priority_queue are often not categorized in containers (container), but instead are categorized in container adapters (container adapter). Priority_queue is a sequential container adapter, and the order container in STL also includes Queue,stack. These three sequential container adapters use the underlying sequential container to do the work they should do.

The above content is summarized from the "STL Source Analysis".

priority_queue Example:

#include <iostream>#include<vector>#include<queue>using namespacestd;intMain () {intia[9] = {0,1,2,3,4,8,9,3,5}; Priority_queue<int, vector<int, greater<int> > Ipq (ia, IA +9);//Compare for greater<int> description Implementation principle is small Gan, can be changed to less<int> observation outputcout <<"size ="<< ipq.size () <<Endl;  for(inti =0; I < ipq.size (); ++i) {cout<< ipq.top () <<" "; } cout<<Endl;  while(!Ipq.empty ()) {cout<< ipq.top () <<" ";    Ipq.pop (); } cout<<Endl; return 0;}

Priority Queue Priority_queue in STL

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.