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;
}
#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;
}
相关文章推荐
- priority_queue 优先队列使用方法
- C++ 标准模板库STL 优先级队列 priority_queue 使用方法与应用介绍(一)
- java中PriorityQueue优先级队列使用方法
- priority_queue使用方法
- java中PriorityQueue优先级队列使用方法
- 优先队列priority_queue的使用方法
- C++ 优先队列 priority_queue 的基本使用方法【定义优先级】
- C++ STL -- stack / queue / priority_queue 的使用方法
- 【转】java中PriorityQueue优先级队列使用方法
- 【STL】c++ priority_queue的使用方法
- C++使用priority_queue方法
- ASINetworkQueue使用cancelAllOperations方法来取消下载时使用的注意点 在 ASIHTTPRequest V1.7前提下 正确使用: imgDataDownLo
- priority_queue 的简单使用方法
- java中PriorityQueue优先级队列使用方法
- java 中PriorityQueue优先级队列使用方法
- priority_queue使用方法
- 优先队列priority_queue容器的使用方法:出队,入队,删除队首元素,判断是否为空
- Java PriorityQueue的使用方法
- priority_queue使用方法
- java中PriorityQueue优先级队列使用方法