stl通用容器之 优先队列

优先队列:

priority_queue类,带优先权的队列,优先权高的元素优先出队。与普通队列相比,共同点都是对队头做删

操作,队尾做插入操作,但不一定遵循先进先出原则,也可能后进先出。priority_queue是一个基于某个基本

列容器进行构建的适配器,默认的序列容器是vector。

常用函数:

(1)构造函数

priority_queue(const Pred& pr = Pred(),const allocator_type& al = allocator_type());创建元素类型为T的空优先队列,Pred是二元比较函数,默认是less<T>

priority_queue(const value_type *first, const value_type *last,

const Pred& pr = Pred(), const allocator_type& al = allocator_type());以迭代器[first,last)指向元素创建元素类型为T的优先

列,Pred是二元比较函数,默认是less<T>

(2)操作函数

队列和堆栈共有函数:

bool empty();如果优先队列为空返回true,否则返回false。

int size(); 返回优先队列中元素数量。

void push(const T& t);t元素压入优先队列。

void pop();当优先队列非空情况下,删除优先级最高元素。

T& top();当优先队列非空情况下,返回优先级最高元素的引用 。

基本操作示例:

#include<iostream>
#include<vector>
#include<queue>
#include<iterator>
#include<algorithm>
using namespace std;
int main()
{
     int a[]={11,3,2,5,5,6,9,8,9,10};
  //   priority_queue<int,vector<int>,greater<int> >pr(a,a+9);  //同过构造函数将a[0]~a[8]送入优先队列
     priority_queue<int>pr(a,a+9);  //同过构造函数将a[0]~a[8]送入优先队列
     pr.push(a[9]); //通过函数把a[9]送入优先队列
     cout<<"进队顺序:";
     copy(a, a+10, ostream_iterator<int>(cout, "\t"));
     cout<<endl;

     cout<<"出队顺序:";
     while(!pr.empty())
     {
          cout<<pr.top()<<"\t";
          pr.pop();
     }
     cout<<endl;
     return 0;
}

示例中priority_queue<int>pr(a,a+9)相当与priority_queue<int,vector<int>,less<int> >pr(a,a+9)。若想升序输出,只需把priority_queue<int>pr(a,a+9)改为priority_queue<int,vector<int>,greater<int> >pr(a,a+9)就可以了。
示例:
显示学生信息(学号、姓名、语文、数学),条件是:总成绩由高到低,当相同语文成绩高占先。

#include<iostream>
#include<queue>
#include<string>
using namespace std;
class Student
{
     int NO;
     string name;
     int chinese;
     int math;
public:
     Student(int N,string nam,int chin,int ma):NO(N),name(nam),chinese(chin),math(ma){}
     int GetNO()
     {
          return NO;
     }
     string GetName()
     {
          return name;
     }
     int GetChinese()
     {
          return chinese;
     }
     int GetMath()
     {
          return math;
     }
     bool operator <(Student &s)  //报错??
     {
          int sum1=chinese+math;
          int chinese2=s.GetChinese();
          int math2=s.GetMath();
          int sum2=chinese2+math2;

          if(sum1<sum2)
          {
               return true;
          }
          if(sum1==sum2 && chinese<chinese2)
          {
               return true;
          }
          return false;
     }

};
int main()
{
     Student s[]={Student(1001,"张3",70,80),Student(1002,"张4",80,70),Student(1003,"张5",85,75),Student(1004,"张6",90,85)};
     priority_queue<Student>pr(s,s+4);
     cout<<"成绩由高到低(当相同时,语文高优先):"<<endl;
     cout<<"学号\t姓名\t语文\t数学"<<endl;
     while(!pr.empty())
     {
          Student &t=pr.top();  //报错?
          cout<<t.GetNO()<<"\t"<<t.GetName()<<"\t"<<t.GetChinese()<<"\t"<<t.GetMath()<<endl;
          pr.pop();
     }
     cout<<endl;
     return 0;
}


设计一个固定大小的优先队列类。即优先队列元素个数只能小于某个值。

分析:标准模板库中priority_queue中元素个数没有限制,与题意不符,那么如何既能用上priority_queue固有功能,又能加上特有的限制

呢,其实在这句话中已经体现出了具体思路:(1)从priority_queue派生出自己定义的优先队列类FixedPriority。 样,FixedPriority类就

继承了priority_queue的固有特性;(2)在FixedPriority类中加入特有的功能,题意中要求限制大小,那么一方面要定义一个成员变量

nLimit,通过构造函数传入优先队列大小,并赋给Limit;另一方面要重载push函数,如果当前堆栈元素个数小于Limit,则把新元素压入堆

栈。

<span style="font-size:18px;">#include<iostream>
#include<queue>
#include<vector>
using namespace std;
template<class T,class Cont=vector<T>,class Pred=less<Cont::value_type> >  // 报错?
class FixedPriority:public priority_queue<T,Cont,Pred>
{
     int Limit;
public:
     FixedPriority(int n):Limit(n){}
     void SetLimitSize(int n)
     {
          Limit=n;
     }
     bool Push(T &t)
     {
          if(Limit>size())
          {
               push(t);
               return true;
          }
          return false;
     }
} ;
int main()
{
     FixedPriority<int>fp(10);
     for(int i=0;i<15;i++)
     {
          if(!fp.Push(i))
          {
               cout<<"优先队列已满!"<<endl;
          }
     }
     return 0;
}</span>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值