优先队列

#include <queue>
using namespace std;
priority_queue<元素类型[[,底层容器类型],比较器类型]>
优先队列对象 (构造实参表);
底层容器:vector/deque(默认)
优者先出,默认以大者为优,也可以通过比较器定制优先级。
首------->尾
50 45 40 30

复制代码
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
class IntCmp {
public:
bool operator() (int a, int b) const{
return a > b;
}
};
class Integer {
public:
Integer (int arg) : m_var (arg) {}
bool operator< (
Integer const& i) const {
//    return m_var < i.m_var;
return m_var > i.m_var;
}
friend ostream& operator<< (
ostream& os, Integer const& i) {
return os << i.m_var;
}
private:
int m_var;
};
int main (void) {
//    priority_queue<int> pq;
//    priority_queue<int, vector<int>,
//    IntCmp> pq;
priority_queue<Integer> pq;
pq.push (40);
pq.push (50);
pq.push (30);
pq.push (60);
pq.push (20);
pq.push (60);
pq.push (40);
while (! pq.empty ()) {
cout << pq.top () << ' ' <<flush;
pq.pop ();
}
cout << endl;
return 0;
}
复制代码

 

posted @ 2018-03-29 10:46  Truman001  阅读(126)  评论(0)    收藏  举报
编辑推荐:
· tomcat为什么假死了
· 聊一聊 Linux 上对函数进行 hook 的两种方式
· C# 锁机制全景与高效实践:从 Monitor 到 .NET 9 全新 Lock
· 一则复杂 SQL 改写后有感
· golang中写个字符串遍历谁不会?且看我如何提升 50 倍
阅读排行:
· 突发,CSDN 崩了!程序员们开始慌了?
· 完成微博外链备案,微博中直接可以打开园子的链接
· C# WinForms 实现打印监听组件
· 推荐 3 种 .NET Windows 桌面应用程序自动更新解决方案
· 一个基于 .NET 开源、模块化 AI 图像生成 Web 用户界面
点击右上角即可分享
微信分享提示