使用priority_queue容器适配器所有操作函数,实现最大值优先队列和最小值优先队列
最大值优先队列
#include <iostream>
using namespace std
;
int main()
{
int a
[] = { 1,2,3,4,5,6,7,8,9,10 };
priority_queue
<int > pr(a
, a
+ 9);
pr
.push(a
[9]);
cout
<< "进队顺序:"; copy(a
, a
+ 10, ostream_iterator
<int>(cout
, " "));cout
<< endl
;
cout
<< "出队顺序:";
while (!pr
.empty())
{
cout
<< pr
.top() << " ";
pr
.pop();
}
cout
<< endl
;
}
进队顺序:5 7 4 2 1 3 6 9 0 8
出队顺序:9 8 7 6 5 4 3 2 1 0
最小值优先队列
int main()
{
int b
[] = { 1,2,3,4,5,6,7,8,9,10 };
priority_queue
<int, vector
<int>, greater
<int> > prb(b
, b
+ 9);
prb
.push(b
[9]);
cout
<< "进队顺序:"; copy(b
, b
+ 10, ostream_iterator
<int>(cout
, " "));cout
<< endl
;
cout
<< "出队顺序:";
while (!prb
.empty())
{
cout
<< prb
.top() << " ";
prb
.pop();
}
}
进队顺序:5 7 4 2 1 3 6 9 0 8
出队顺序:0 1 2 3 4 5 6 7 8 9
转载请注明原文地址: https://ju.6miu.com/read-678481.html