设计一个固定大小的队列类
#include <iostream>
#include <queue>
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 nLimit;
public:
FixedPriority(int nLimit)
{
this->nLimit = nLimit;
}
void SetLimitSize(int nLimit)
{
this->nLimit = nLimit;
}
bool Push(T& t)
{
if (nLimit > size())
{
push(t);
return true;
}
return false;
}
};
void main()
{
FixedPriority<int> fp(10);
for (int i = 0; i<15; i++)
{
if (!fp.Push(i))
{
cout << "优先队列已满,第" << i << "个元素没有插入" << endl;
}
}
cout << endl;
cout << "打印队列内容:" << endl;
while (!fp.empty())
{
cout << fp.top() << "\t";
fp.pop();
}
system("pause");
}