template< class T ,
class Sequence=vector ,
class Compare=less >
#include
#include
#include
using namespace std;
struct node {
int no;
int year;
int period;
int current;
};
int num;
struct cmp {
bool operator() (const node &a, const node &b)
{
if (a.current < b.current)
return false;
else if (a.current == b.current)
return (a.year > b.year);
else return true;
}
};
priority_queue, cmp> p;
void slove()
{
while (num--)
{
node e = p.top();
p.pop();
printf("%d/n", e.year);
e.current += e.period;
p.push(e);
}
}
int main()
{
node a;
char s[100];
for (int i=0; scanf("%s", s) && strcmp(s, "#"); i++)
{
scanf("%d%d", &a.year, &a.period);
a.current = a.period;
a.no = i;
p.push(a);
}
scanf("%d", &num);
slove();
return 0;
}
#include #include #include using namespace std; struct node { int x; int y; int w; friend bool operator> (const node& a,const node& b) { return (a.w > b.w); } friend bool operator< (const node& a,const node& b) { return (a.w < b.w); } }; int main() { int i; priority_queue < node, vector, greater > que1; //priority_queue < node, vector, greater>(两个连在一起不行) que; priority_queue < node, vector, less > que2; node da[5]; for(i = 0;i < 5;i++) { da[i].x = i; da[i].y = i; da[i].w = i; que1.push(da[i]); que2.push(da[i]); } while(!que1.empty()) { cout<
#include #include #include using namespace std; struct node { int x; int y; int w; }; struct node_greater_cmp { bool operator()(const node& a,const node& b) { return a.w > b.w; } }; struct node_less_cmp { bool operator()(const node& a,const node& b) { return a.w < b.w; } }; int main() { int i; priority_queue < node, vector, node_greater_cmp > que1; priority_queue < node, vector, node_less_cmp > que2; node da[5]; for(i = 0;i < 5;i++) { da[i].x = i; da[i].y = i; da[i].w = i; que1.push(da[i]); que2.push(da[i]); } while(!que1.empty()) { cout<
class Enity { public: int mId; std::string mName; }; class Lesser { public: bool operator () (Enity a, Enity b) { return a.mId > b.mId; } }; int _tmain(int argc, _TCHAR* argv[]) { priority_queue, Lesser> q; Enity a, b, c; a.mId = 2; b.mId = 1; c.mId = 3; q.push(a); q.push(b); q.push(c); cout << q.top().mId << endl; q.pop(); cout << q.top().mId << endl; q.pop(); cout << q.top().mId << endl; q.pop(); return 0; }
priority_queue用法详解v2
2009年7月3日
creke
大部分内容来自某STL语法详解文档,贴出来应该没问题吧~~
1.先给一个简单应用的例子,这个和容器的用法差不多。
#include
#include
using namespace std;
int main()
{
priority_queue<float> q; //默认的是大顶堆
// insert three elements into the priority queue
q.push(66.6);
q.push(22.2);
q.push(44.4);
// read and print two elements
cout << q.top() << ' '; //queue当中是q.front();
q.pop();
cout << q.top() << endl;
q.pop();
// insert three more elements
q.push(11.1);
q.push(55.5);
q.push(33.3);
// skip one element
q.pop();
// pop and print remaining elements
while (!q.empty())
{
cout << q.top() << ' ';
q.pop();
}
cout << endl;
}
2.下面是将节点存在优先队列中的两种方式
最好的方式:这个简洁!
struct Node
{
int x,y;
bool operator <(Node a) const { return y < a.y; }
bool operator >(Node a) const { return y > a.y; }
};
priority_queue A; //大根堆
priority_queue, greater > B; //小根堆
方式一:
struct Node
{int adj;
int val;
friend bool operator<(const Node &a,const Node &b) { return a.val > b.val; }
};
priority_queueQ;
方式二:(cmp将结构体以val由大到小排列,组成大根堆)一般也用这个!
struct Node
{int adj;
int val;
};
struct cmp
{bool operator()(Node a,Node b) { return a.val > b.val; }
};
priority_queue,cmp>Q;
方式三:
struct TMax
{
TMax(int tx):x(tx){}
int x;
};
struct TMin
{
TMin(int tx):x(tx){}
int x;
};
bool operator<(const TMax &a, const TMax &b)
{
return a.xbool operator<(const TMin &a, const TMin &b)
{
return a.x>b.x;
}
priority_queue hmax; //大顶堆
priority_queue hmin; //小顶堆
3.下面是将指针存在优先队列中的方式
struct Node
{
short f;
short d;
short fishs;
short times;
short i;
};
struct PCmp
{
bool operator () (Node const *x, Node const *y)
{
if(x->f == y->f)
return x->i > y->i;
return x->f < y->f;
}
};
priority_queue, PCmp > Q;
注:在这种情况下往往可以预分配空间以避免new带来的麻烦。例如:堆中定义Node Qt[26], 栈中的使用则为Node *tmp1 = Qt。
经过测试,在优选队列当中存指针进行一系列操作要比存节点进行一系列操作快一些。
注:
- less这是大顶堆,按值大的优先,值大的在最上面。greater这是小顶堆,按值小的优先,值小的在最上面。
- 自定义cmp如果还有不明白的看这里:
struct cmp
{
bool operator()(const int &a,const int &b)//这里的&是引用
{
return a>b;//最大堆
return a//最小堆
}
};
priority_queue< int, vector<int>, cmp >
还是自定义cmp函数,注意,一般ACM中用结构体内含“bool operator()(const int &a,const int &b)”。这其实等价于Class cmp,不过更省事,当然也不规范(不需要规范)。 return就是希望如何排列为true。如果希望由大到小,就将大到小的情况return;反则亦然。和sort的自定义cmp是一样的。