其实priority_queue的正确用法:
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
class Node
{
public:
Node(){Node(0);}
Node(int v): m_iVar(v){};
~Node(){}
bool operator < (Node& t);
int getVar();
private:
int m_iVar;
};
bool Node:: operator < (Node& t)
{
return this->m_iVar < t.m_iVar;
}
int Node:: getVar()
{
return m_iVar;
}
template<class T>
class Compare
{
public:
bool operator () (const T a, const T b); //比较指针大小
private:
};
/*
快看看这个是是有多怪吧!
*/
template<class T>
bool Compare<T>:: operator () (const T a, const T b)
{
return *a < *b;
}
int main()
{
priority_queue<Node*, vector<Node*>, Compare<Node*> > pq;
Node a(1), b(2), c(3);
pq.push(&a);
pq.push(&b);
pq.push(&c);
while (!pq.empty()) {
cout << pq.top()->getVar() << endl;
pq.pop();
}
return 0;
}