[STL]priority_queue


由于merge k sorted lists要用到优先队列,所以参看各种博客。现在总结一下。

按默认规定,priority_queue简单地用<<script type="math/tex" id="MathJax-Element-3"><</script>运算符做元素比较。

根据这里:

class mycomparison
{bool reverse;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);}
};void testPQ()
{int myints[]= {10,60,50,20};priority_queue<int> first;priority_queue<int> second (myints,myints+4);priority_queue<int, std::vector<int>, std::greater<int> >third (myints,myints+4);// using mycomparison:typedef std::priority_queue<int,vector<int>,mycomparison> mypq_type;mypq_type fourth(myints,myints+4);                       // less-than comparisonmypq_type fifth (myints,myints+4, mycomparison(true));   // greater-than comparison//emptycout << "first: " << endl;while (!first.empty()){cout << first.top() << " ";first.pop();}cout << endl;//default maxHeapcout << "second: " << endl;while (!second.empty()){cout << second.top() << " ";second.pop();}cout << endl;//minHeapcout << "third: " << endl;while (!third.empty()){cout << third.top() << " ";third.pop();}cout << endl;//less than maxHeap-DIYcout << "fourth: " << endl;while (!fourth.empty()){cout << fourth.top() << " ";fourth.pop();}cout << endl;//greater than minHeap-DIYcout << "fifth: " << endl;while (!fifth.empty()){cout << fifth.top() << " ";fifth.pop();}cout << endl;
}

程序的输出结果:

first为空,second默认为最大堆,third用greater(要include <functional>)变成最小堆,fourth和fifth用的都是自定义的比较函数,mycomparison是一个类,重载operator()。如果参数为true,即lhs>rhs,即greater,为最小堆。

对于自定义的类型,也同样可以插入优先队列。比如现在要把

struct Node
{int val;Node* next;Node(int x) : val(x), next(NULL) {}
};

根据val值插入到最小堆(当然更准确的描述应该是val值越小,越先到达top())。

有两种方法:

  1. 改变node结构体,重载<<script type="math/tex" id="MathJax-Element-4"><</script>,修改成:
struct Node
{int val;Node* next;bool operator < (const Node &x) const {return val > x.val;}Node(int x) : val(x), next(NULL) {}
};

测试函数:

void testPq()
{priority_queue<Node, vector<Node>> pq;int a[6] = {5, 6, 8, 10, 230, 0};for (int i = 0; i < 6; i++)pq.push(Node(a[i]));while (!pq.empty()){Node temp = pq.top();pq.pop();cout << temp.val << " ";}cout << endl;
}

输出为:

2. 重新定义一个比较类。

class nodeComparison
{//此函数要加上public。//“>”说明是最小堆。“<”说明是最大堆
public: bool operator() (Node &n1, Node &n2){return n1.val > n2.val;}
};

测试函数同上。初始化priority_queue的时候有一点不同:

priority_queue<Node, vector<Node>, nodeComparison> pq;

如果要push进priority_queue的是Node型指针。则要定义一个比较类了:

class nodeComparison
{//此函数要加上public。//“>”说明是最小堆。“<”说明是最大堆
public: bool operator() (Node* n1, Node* n2){return n1->val > n2->val;}
};

综上,如果是基本内置类型,可以用less,greter或者再定义一个比较类,重载operator()。如果是自定义类型,则直接定义一个比较类。The CPP Languages P414,424

参考:
http://www.cnblogs.com/cszlg/archive/2012/07/27/2611607.html

[STL]priority_queue相关推荐

  1. STL Priority_Queue

    priority_queue 调用 STL里面的 make_heap(), pop_heap(), push_heap() 算法 实现,也算是堆的另外一种形式. 先写一个用 STL 里面堆算法实现的与 ...

  2. STL priority_queue sort 自定义比较终极模板

    比较有两种重载,一种是类内部的bool operator<( 只有一个参数 ),当然bool operator< 也可以拿到类的外面:另外一种是写一个cmp,利用cmp返回作为sort的第 ...

  3. STL:priority_queue

    模板原型:priority_queue<T,Sequence,Compare> T:存放容器的元素类型 Sequence:实现优先级队列的底层容器,默认是vector<T> C ...

  4. C++ STL priority_queue的正确使用方法

    priority_queue相对于queue的不同之处在于:优先队列实现了内部自动排序,可根据自己需要自定义排序规则,可以自己编写函数或者仿函数用于内部优先级的确定. //priority_queue ...

  5. STL之优先级队列priority_queue

    摘要: priority_queue,自适应容器(即容器适配器):不能由list来组建: 最大值优先级队列(最大值始终在对首,push进去时候) 最小值优先级队列: 优先级队列适配器 STL  pri ...

  6. c++STL容器的priority_queue

    TL容器的priority_queue STL容器的priority_queue的简介 STL容器的priority_queue的简介 最大值优先级队列.最小值优先级队列 优先级队列适配器 STL ...

  7. STL学习系列七:优先级队列priority_queue容器

    1.简介 最大值优先级队列.最小值优先级队列 优先级队列适配器 STL priority_queue 用来开发一些特殊的应用,请对stl的类库,多做扩展性学习 这里给个例子: #include< ...

  8. C++ STL 学习笔记__(6)优先级队列priority_queue基本操作

    10.2.7优先级队列priority_queue v  最大值优先级队列.最小值优先级队列 v  优先级队列适配器 STL priority_queue v  用来开发一些特殊的应用,请对stl的类 ...

  9. STL容器底层数据结构的实现

    C++ STL 的实现: 1.vector      底层数据结构为数组 ,支持快速随机访问 2.list            底层数据结构为双向链表,支持快速增删 3.deque       底层 ...

最新文章

  1. MATLAB_10-模式识别_
  2. ubuntu设置始终亮屏_如何设置默认显示亮度?
  3. lr分析器的设计与实现实验报告_《计算机导论》课程实验报告(一维数组)
  4. 数据采集录入填报时如何只更新当前修改行
  5. 【论文阅读】Learning Traffic as Images: A Deep Convolutional ... [将交通作为图像学习: 用于大规模交通网络速度预测的深度卷积神经网络](1)
  6. 【遥感数字图像处理】实验:遥感图像分析方法大全(Erdas版)
  7. 【转】[iOS] 关于 self = [super init];
  8. 改进初学者的PID-微分冲击
  9. 极大似然估计和贝叶斯估计
  10. Spring事务管理全面分析
  11. HttpClientHelper的封装
  12. mysql忘记了密码、允许远程连接、mysql卸载 -- linux
  13. ubuntu配置LAMP
  14. 分区桌面 壁纸(正在做,常用,之后在做,临时存)
  15. kodi android 目录,Kodi使用豆瓣刮削器建立媒体库,以及把资料库导出到片源目录...
  16. 思维导图与知识树的区别
  17. 《A Relation-Specific Attention Network for Joint Entity and Relation Extraction》论文
  18. 大学生创新创业大赛案例_七大学生创新创业大赛样本示例
  19. php加密---六种加密方式
  20. Week 7 Homework

热门文章

  1. ubuntu tail、history|grep 、alias命令
  2. Linux下main函数带参数问题和atoi函数详解
  3. 开始学习3年前的东西——MCMS
  4. LOJ#2353 货币兑换
  5. UDP接收端和发送端_Socket编程
  6. 彻底卸载WinStdup
  7. zabbixproxy安装
  8. 上海网域CEO肖确伟:IDC精细化运营探讨
  9. 《数据结构与抽象:Java语言描述(原书第4版)》一练习
  10. 【十三单元】 软件安装 yum源配置