Queue是STL中的队列表示。
允许的操作是新增元素,移除元素,从最底端添加元素,从最顶端获取元素,除了从底端加入元素,从顶端取出外,没有任何其它方法可以存取queue的其他元素,也就是说queue不允许有遍历行为。
把元素推入queue的操作为push, 将元素推出queue的操作为pop.
由于queue系以底部容器完成其所有工作,而具有这种“修改某物接口,形成另外一种风貌”的性质的,称为adapter(容器),因此,STL queue往往不被归类为container(容器),而被称为 container adapter.
queue的底层容器的函数有empty, size, front, back, push_front, push_back, pop_front, pop_back。所以假如用list为底部容器,封闭某些接口,能很轻易的行程一个queue。
下面是queue的部分源码,可以从里面看下queue的一些实现和方法定义。
我在看侯捷的STL源码解析的时候,里面说其实queue的方法简单,把list
的一些接口给封掉,就可以轻松的实现queue的。具体用list
实现的代码,可以看《STL源码剖析》这本书。
/*
queue synopsis
namespace std
{
template <class T, class Container = deque<T>>
class queue
{
public:
typedef Container container_type;
typedef typename container_type::value_type value_type;
typedef typename container_type::reference reference;
typedef typename container_type::const_reference const_reference;
typedef typename container_type::size_type size_type;
protected:
container_type c;
public:
queue() = default;
~queue() = default;
queue(const queue& q) = default;
queue(queue&& q) = default;
queue& operator=(const queue& q) = default;
queue& operator=(queue&& q) = default;
explicit queue(const container_type& c);
explicit queue(container_type&& c)
template <class Alloc>
explicit queue(const Alloc& a);
template <class Alloc>
queue(const container_type& c, const Alloc& a);
template <class Alloc>
queue(container_type&& c, const Alloc& a);
template <class Alloc>
queue(const queue& q, const Alloc& a);
template <class Alloc>
queue(queue&& q, const Alloc& a);
bool empty() const;
size_type size() const;
reference front();
const_reference front() const;
reference back();
const_reference back() const;
void push(const value_type& v);
void push(value_type&& v);
template <class... Args> void emplace(Args&&... args);
void pop();
void swap(queue& q) noexcept(noexcept(swap(c, q.c)));
};
template <class T, class Container>
bool operator==(const queue<T, Container>& x,const queue<T, Container>& y);
template <class T, class Container>
bool operator< (const queue<T, Container>& x,const queue<T, Container>& y);
template <class T, class Container>
bool operator!=(const queue<T, Container>& x,const queue<T, Container>& y);
template <class T, class Container>
bool operator> (const queue<T, Container>& x,const queue<T, Container>& y);
template <class T, class Container>
bool operator>=(const queue<T, Container>& x,const queue<T, Container>& y);
template <class T, class Container>
bool operator<=(const queue<T, Container>& x,const queue<T, Container>& y);
template <class T, class Container>
void swap(queue<T, Container>& x, queue<T, Container>& y)
noexcept(noexcept(x.swap(y)));
template <class T, class Container = vector<T>,
class Compare = less<typename Container::value_type>>
class priority_queue
{
public:
typedef Container container_type;
typedef typename container_type::value_type value_type;
typedef typename container_type::reference reference;
typedef typename container_type::const_reference const_reference;
typedef typename container_type::size_type size_type;
protected:
container_type c;
Compare comp;
public:
priority_queue() = default;
~priority_queue() = default;
priority_queue(const priority_queue& q) = default;
priority_queue(priority_queue&& q) = default;
priority_queue& operator=(const priority_queue& q) = default;
priority_queue& operator=(priority_queue&& q) = default;
explicit priority_queue(const Compare& comp);
priority_queue(const Compare& comp, const container_type& c);
explicit priority_queue(const Compare& comp, container_type&& c);
template <class InputIterator>
priority_queue(InputIterator first, InputIterator last,
const Compare& comp = Compare());
template <class InputIterator>
priority_queue(InputIterator first, InputIterator last,
const Compare& comp, const container_type& c);
template <class InputIterator>
priority_queue(InputIterator first, InputIterator last,
const Compare& comp, container_type&& c);
template <class Alloc>
explicit priority_queue(const Alloc& a);
template <class Alloc>
priority_queue(const Compare& comp, const Alloc& a);
template <class Alloc>
priority_queue(const Compare& comp, const container_type& c,
const Alloc& a);
template <class Alloc>
priority_queue(const Compare& comp, container_type&& c,
const Alloc& a);
template <class Alloc>
priority_queue(const priority_queue& q, const Alloc& a);
template <class Alloc>
priority_queue(priority_queue&& q, const Alloc& a);
bool empty() const;
size_type size() const;
const_reference top() const;
void push(const value_type& v);
void push(value_type&& v);
template <class... Args> void emplace(Args&&... args);
void pop();
void swap(priority_queue& q)
noexcept(noexcept(swap(c, q.c)) && noexcept(swap(comp.q.comp)));
};
template <class T, class Container, class Compare>
void swap(priority_queue<T, Container, Compare>& x,
priority_queue<T, Container, Compare>& y)
noexcept(noexcept(x.swap(y)));
} // std
*/
原文地址:http://blog.csdn.net/alps1992/article/details/44158431