### 前言
queue是一种“先进先出”的数据结构,可以对两端进行操作,但是只能在队列头部进行移除元素,只能在队列尾部新增元素,可以访问队列尾部和头部的元素,但是不能遍历容器,所以queue不需要设计自己的容器。在SGI STL的源码<stl_queue.h>的class queue设计中,它是基于某种容器作为底部结构的,默认容器是deque容器,用户也可以自己指定容器的类型。
### queue容器配接器
源码剖析如下:
~~~
#ifndef __SGI_STL_INTERNAL_QUEUE_H
#define __SGI_STL_INTERNAL_QUEUE_H
#include <sequence_concepts.h>
__STL_BEGIN_NAMESPACE
// Forward declarations of operators < and ==, needed for friend declaration.
//默认底层容器为deque容器
template <class _Tp,
class _Sequence __STL_DEPENDENT_DEFAULT_TMPL(deque<_Tp>) >
class queue;
template <class _Tp, class _Seq>
inline bool operator==(const queue<_Tp, _Seq>&, const queue<_Tp, _Seq>&);
template <class _Tp, class _Seq>
inline bool operator<(const queue<_Tp, _Seq>&, const queue<_Tp, _Seq>&);
template <class _Tp, class _Sequence>
class queue {
// requirements:
__STL_CLASS_REQUIRES(_Tp, _Assignable);
__STL_CLASS_REQUIRES(_Sequence, _FrontInsertionSequence);
__STL_CLASS_REQUIRES(_Sequence, _BackInsertionSequence);
typedef typename _Sequence::value_type _Sequence_value_type;
__STL_CLASS_REQUIRES_SAME_TYPE(_Tp, _Sequence_value_type);
#ifdef __STL_MEMBER_TEMPLATES
template <class _Tp1, class _Seq1>
friend bool operator== (const queue<_Tp1, _Seq1>&,
const queue<_Tp1, _Seq1>&);
template <class _Tp1, class _Seq1>
friend bool operator< (const queue<_Tp1, _Seq1>&,
const queue<_Tp1, _Seq1>&);
#else /* __STL_MEMBER_TEMPLATES */
friend bool __STD_QUALIFIER
operator== __STL_NULL_TMPL_ARGS (const queue&, const queue&);
friend bool __STD_QUALIFIER
operator< __STL_NULL_TMPL_ARGS (const queue&, const queue&);
#endif /* __STL_MEMBER_TEMPLATES */
public:
// queue仅支持对头部和尾部的操作, 所以不定义STL要求的
// pointer, iterator, difference_type
typedef typename _Sequence::value_type value_type;
typedef typename _Sequence::size_type size_type;
typedef _Sequence container_type;
typedef typename _Sequence::reference reference;
typedef typename _Sequence::const_reference const_reference;
protected:
_Sequence c;//底层容器,默认为deque容器,用户可自行指定容器类型
public:
//下面对queue的维护完全依赖于底层容器的操作
queue() : c() {}
explicit queue(const _Sequence& __c) : c(__c) {}
//判断容器是否为空
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 <class _Tp, class _Sequence>
bool
operator==(const queue<_Tp, _Sequence>& __x, const queue<_Tp, _Sequence>& __y)
{
return __x.c == __y.c;
}
template <class _Tp, class _Sequence>
bool
operator<(const queue<_Tp, _Sequence>& __x, const queue<_Tp, _Sequence>& __y)
{
return __x.c < __y.c;
}
#ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
template <class _Tp, class _Sequence>
bool
operator!=(const queue<_Tp, _Sequence>& __x, const queue<_Tp, _Sequence>& __y)
{
return !(__x == __y);
}
template <class _Tp, class _Sequence>
bool
operator>(const queue<_Tp, _Sequence>& __x, const queue<_Tp, _Sequence>& __y)
{
return __y < __x;
}
template <class _Tp, class _Sequence>
bool
operator<=(const queue<_Tp, _Sequence>& __x, const queue<_Tp, _Sequence>& __y)
{
return !(__y < __x);
}
template <class _Tp, class _Sequence>
bool
operator>=(const queue<_Tp, _Sequence>& __x, const queue<_Tp, _Sequence>& __y)
{
return !(__x < __y);
}
#endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
~~~
下面举例说明:
~~~
// constructing queues
#include <iostream> // std::cout
#include <deque> // std::deque
#include <list> // std::list
#include <queue> // std::queue
int main ()
{
std::deque<int> mydeck (3,100); // deque with 3 elements
std::list<int> mylist (2,200); // list with 2 elements
std::queue<int> first; // empty queue
std::queue<int> second (mydeck); // queue initialized to copy of deque
std::queue<int,std::list<int> > third; // empty queue with list as underlying container
std::queue<int,std::list<int> > fourth (mylist);
std::cout << "size of first: " << first.size() << '\n';
std::cout << "size of second: " << second.size() << '\n';
std::cout << "size of third: " << third.size() << '\n';
std::cout << "size of fourth: " << fourth.size() << '\n';
std::cout << "The element at the front of queue second is: "
<< second.front( ) << std::endl;
second.push(10);
std::cout << "The element at the back of queue second is: "
<< second.back( ) << std::endl;
return 0;
}
Output:
size of first: 0
size of second: 3
size of third: 0
size of fourth: 2
The element at the front of queue second is: 100
The element at the back of queue second is: 10
~~~
参考资料:
《STL源码剖析》侯捷
- 前言
- 空间配置器
- Traits编程技术
- STL源码剖析——迭代器
- 全局函数construct(),destroy(),uninitialized_copy(),uninitialized_fill(),uninitialized_fill_n()
- 序列容器之vector
- list容器的排序算法sort()
- 序列容器之list
- 序列容器之deque
- 容器配接器之stack
- 容器配接器之queue
- 容器配接器之priority_queue
- 最大堆heap
- 单向链表slist
- RB-Tree(红黑树)
- 关联容器之set
- stl_pair.h学习
- 关联容器之map
- 关联容器之multiset
- 关联容器之multimap
- 散列表hashtable
- stl_hash_fun.h学习
- 关联容器之hash_set
- 关联容器之hash_multiset
- 关联容器之hash_map
- 关联容器之hash_multimap
- 数值算法stl_numeric.h
- stl_relops.h学习
- 基本算法stl_algobase.h
- STL算法之set集合算法
- STL算法stl_algo.h
- STL算法之sort排序算法
- STL算法之find查找算法
- STL算法之merge合并算法
- STL算法之remove删除算法
- STL算法之permutation排列组合
- STL函数对象