#include <iostream>
#include <deque>
using namespace std;
template <typename T, typename Sequence = deque<T>>
class queue
{
friend bool operator==(const queue &x, const queue &y);
friend bool operator<(const queue &x, const queue &y);
public:
typedef typename Sequence::value_type value_type;
typedef typename Sequence::size_type size_type;
typedef typename Sequence::reference reference;
typedef typename Sequence::const_reference const_reference;
protected:
Sequence c;
public:
queue() : c(){}
queue(const Sequence &x) : c(x){}
// 以下是STL queue的标准接口 都调用deque的成员函数实现
bool empty() const
{
return c.empty();
}
size_type size() const
{
return c.size();
}
reference front()
{
return c.front();
}
const_reference front() const
{
return c.front();
}
reference back()
{
return c.back();
}
const_reference back() const
{
return c.back();
}
void push(const value_type &x)
{
c.push_back(x);
}
void pop()
{
c.pop_front();
}
};
template <typename T, typename Sequence>
bool operator==(const queue<T, Sequence> &x, const queue<T, Sequence> &y)
{
return x.c == y.c;
}
template <typename T, typename Sequence>
bool operator<(const queue<T, Sequence> &x, const queue<T, Sequence> &y)
{
return x.c < y.c;
}
template <typename T, typename Sequence>
bool operator!=(const queue<T, Sequence> &x, const queue<T, Sequence> &y)
{
return !(x == y);
}
template <typename T, typename Sequence>
bool operator>(const queue<T, Sequence> &x, const queue<T, Sequence> &y)
{
return (y < x);
}
template <typename T, typename Sequence>
bool operator<=(const queue<T, Sequence> &x, const queue<T, Sequence> &y)
{
return !(y < x);
}
template <typename T, typename Sequence>
bool operator>=(const queue<T, Sequence> &x, const queue<T, Sequence> &y)
{
return !(x < y);
}
int main()
{
queue<int> intQueue;
for (int i = 0; i != 30; ++i)
{
intQueue.push(i);
}
while (!intQueue.empty())
{
int value = intQueue.front();
intQueue.pop();
cout << value << " ";
}
cout << intQueue.size() << endl;
return 0;
}
queue(仿sgi stl)
最新推荐文章于 2022-02-11 22:27:23 发布