STL系列之三 queue 单向队列

queue单向队列与有点类似,一个是在同一端存取数据,另一个是在一端存入数据,另一端取出数据。单向队列中的数据是先进先出(First In First Out,FIFO)。在STL中,单向队列也是以别的容器作为底部结构,再将接口改变,使之符合单向队列的特性就可以了。因此实现也是非常方便的。下面就给出单向队列的函数列表和VS2008中单向队列的源代码。单向队列一共6个常用函数(front()、back()、push()、pop()、empty()、size()),与的常用函数较为相似。

 

VS2008中queue单向队列的源代码

友情提示:初次阅读时请注意其实现思想,不要在细节上浪费过多的时间。

//VS2008中 queue的定义 MoreWindows整理(http://blog.csdn.net/MoreWindows)
template<class _Ty, class _Container = deque<_Ty> >
class queue
{	// FIFO queue implemented with a container
public:
	typedef _Container container_type;
	typedef typename _Container::value_type value_type;
	typedef typename _Container::size_type size_type;
	typedef typename _Container::reference reference;
	typedef typename _Container::const_reference const_reference;

	queue() : c()
	{	// construct with empty container
	}

	explicit queue(const _Container& _Cont) : c(_Cont)
	{	// construct by copying specified container
	}

	bool empty() const
	{	// test if queue is empty
		return (c.empty());
	}

	size_type size() const
	{	// return length of queue
		return (c.size());
	}

	reference front()
	{	// return first element of mutable queue
		return (c.front());
	}

	const_reference front() const
	{	// return first element of nonmutable queue
		return (c.front());
	}

	reference back()
	{	// return last element of mutable queue
		return (c.back());
	}

	const_reference back() const
	{	// return last element of nonmutable queue
		return (c.back());
	}

	void push(const value_type& _Val)
	{	// insert element at beginning
		c.push_back(_Val);
	}

	void pop()
	{	// erase element at end
		c.pop_front();
	}

	const _Container& _Get_container() const
	{	// get reference to container
		return (c);
	}

protected:
	_Container c;	// the underlying container
};

可以看出,由于queue只是进一步封装别的数据结构,并提供自己的接口,所以代码非常简洁,如果不指定容器,默认是用deque来作为其底层数据结构的(对deque不是很了解?可以参阅《STL系列之一deque双向队列》)。下面给出单向队列的使用范例:

//单向队列 queue支持 empty() size() front() back() push() pop()
//By MoreWindows(http://blog.csdn.net/MoreWindows)
#include <queue>
#include <vector>
#include <list>
#include <cstdio>
using namespace std;

int main()
{
	//可以使用list作为单向队列的容器,默认是使用deque的。
	queue<int, list<int>> a;
	queue<int>        b;
	int i;

	//压入数据
	for (i = 0; i < 10; i++)
	{
		a.push(i);
		b.push(i);
	}

	//单向队列的大小
	printf("%d %d\n", a.size(), b.size());

	//队列头和队列尾
	printf("%d %d\n", a.front(), a.back());
	printf("%d %d\n", b.front(), b.back());

	//取单向队列项数据并将数据移出单向队列
	while (!a.empty())
	{
		printf("%d ", a.front());
		a.pop();
	}
	putchar('\n');

	while (!b.empty())
	{
		printf("%d ", b.front());
		b.pop();
	}
	putchar('\n');
	return 0;
}

 

转载请标明出处,原文地址:http://blog.csdn.net/morewindows/article/details/6950917

 

  • 30
    点赞
  • 59
    收藏
    觉得还不错? 一键收藏
  • 12
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值