优先队列

#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  阅读(130)  评论(0)    收藏  举报