您的位置:首页 > 产品设计 > UI/UE

priority_queue的正确使用方法

2014-04-13 22:28 344 查看
其实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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++