C++进阶——STL源码之顺序容器适配器

顺序容器适配器

容器适配器是一种利用存在的容器类型采用另一种不同的抽象类型的工作方式实现。

stack

stack是一种先进后出的数据结构,它只有一个出口;stack允许新增元素、移除元素、取得最顶端的元素;但是除了顶端外不可以存取其他元素;stack没有迭代器。

stack在缺省的情况下是以deque作为底部容器来完成所有的工作,元素的操作只有push 和 pop 两个接口。

stack也可以使用vector 、 list 、deque 作为底部容器。

template<typename _Tp, typename _Sequence = deque<_Tp> >
class stack
{
	// concept requirements
	typedef typename _Sequence::value_type _Sequence_value_type;
	__glibcxx_class_requires(_Tp, _SGIAssignableConcept)
		__glibcxx_class_requires(_Sequence, _BackInsertionSequenceConcept)
		__glibcxx_class_requires2(_Tp, _Sequence_value_type, _SameTypeConcept)

		template<typename _Tp1, typename _Seq1>
	friend bool
		operator==(const stack<_Tp1, _Seq1>&, const stack<_Tp1, _Seq1>&);

	template<typename _Tp1, typename _Seq1>
	friend bool
		operator<(const stack<_Tp1, _Seq1>&, const stack<_Tp1, _Seq1>&);

public:
	typedef typename _Sequence::value_type                value_type;
	typedef typename _Sequence::reference                 reference;
	typedef typename _Sequence::const_reference           const_reference;
	typedef typename _Sequence::size_type                 size_type;
	typedef          _Sequence                            container_type;

protected:
.
	_Sequence c;

public:

	explicit
		stack(const _Sequence& __c = _Sequence())
		: c(__c) { }

	bool
		empty() const
	{
		return c.empty();
	}

	size_type
		size() const
	{
		return c.size();
	}


	reference
		top()
	{
		__glibcxx_requires_nonempty();
		return c.back();
	}


	const_reference
		top() const
	{
		__glibcxx_requires_nonempty();
		return c.back();
	}


	void
		push(const value_type& __x)
	{
		c.push_back(__x);
	}



	void
		pop()
	{
		__glibcxx_requires_nonempty();
		c.pop_back();
	}

};

queue

queue是一种先进先出的数据结构,他有两个接口;queue允许新增元素、移除元素、从最低端加入元素、从最顶端取得元素;queue也没有迭代器。

queue缺省的情况下以deque作为底部容器实现;

queue可以使用 deque 、 list 作为底部实现容器。

 

template<typename _Tp, typename _Sequence = deque<_Tp> >
class queue
{
	// concept requirements
	typedef typename _Sequence::value_type _Sequence_value_type;
	__glibcxx_class_requires(_Tp, _SGIAssignableConcept)
		__glibcxx_class_requires(_Sequence, _FrontInsertionSequenceConcept)
		__glibcxx_class_requires(_Sequence, _BackInsertionSequenceConcept)
		__glibcxx_class_requires2(_Tp, _Sequence_value_type, _SameTypeConcept)

		template<typename _Tp1, typename _Seq1>
	friend bool
		operator==(const queue<_Tp1, _Seq1>&, const queue<_Tp1, _Seq1>&);

	template<typename _Tp1, typename _Seq1>
	friend bool
		operator<(const queue<_Tp1, _Seq1>&, const queue<_Tp1, _Seq1>&);

public:
	typedef typename _Sequence::value_type                value_type;
	typedef typename _Sequence::reference                 reference;
	typedef typename _Sequence::const_reference           const_reference;
	typedef typename _Sequence::size_type                 size_type;
	typedef          _Sequence                            container_type;

protected:

	_Sequence c;

public:

	explicit
		queue(const _Sequence& __c = _Sequence())
		: c(__c) { }


	/**
	*  Returns true if the %queue is empty.
	*/
	bool
		empty() const
	{
		return c.empty();
	}

	/**  Returns the number of elements in the %queue.  */
	size_type
		size() const
	{
		return c.size();
	}

	/**
	*  Returns a read/write reference to the data at the first
	*  element of the %queue.
	*/
	reference
		front()
	{
		__glibcxx_requires_nonempty();
		return c.front();
	}

	/**
	*  Returns a read-only (constant) reference to the data at the first
	*  element of the %queue.
	*/
	const_reference
		front() const
	{
		__glibcxx_requires_nonempty();
		return c.front();
	}

	/**
	*  Returns a read/write reference to the data at the last
	*  element of the %queue.
	*/
	reference
		back()
	{
		__glibcxx_requires_nonempty();
		return c.back();
	}

	/**
	*  Returns a read-only (constant) reference to the data at the last
	*  element of the %queue.
	*/
	const_reference
		back() const
	{
		__glibcxx_requires_nonempty();
		return c.back();
	}

	void
		push(const value_type& __x)
	{
		c.push_back(__x);
	}


	void
		pop()
	{
		__glibcxx_requires_nonempty();
		c.pop_front();
	}

};

 

priority_queue

priority_queue是一个拥有权值观念的queue,它允许加入新的元素、移除旧元素、审视元素等功能;

priority_queue也是一种queue,它只允许在底部加入元素,在顶部取出元素;

priority_queue缺省的情况下是以vector作为底部容器实现;

priority_queue 没有迭代器。

template<typename _Tp, typename _Sequence = vector<_Tp>,
	typename _Compare = less<typename _Sequence::value_type> >
	class priority_queue
{
	// concept requirements
	typedef typename _Sequence::value_type _Sequence_value_type;
	__glibcxx_class_requires(_Tp, _SGIAssignableConcept)
		__glibcxx_class_requires(_Sequence, _SequenceConcept)
		__glibcxx_class_requires(_Sequence, _RandomAccessContainerConcept)
		__glibcxx_class_requires2(_Tp, _Sequence_value_type, _SameTypeConcept)
		__glibcxx_class_requires4(_Compare, bool, _Tp, _Tp,
			_BinaryFunctionConcept)

public:
	typedef typename _Sequence::value_type                value_type;
	typedef typename _Sequence::reference                 reference;
	typedef typename _Sequence::const_reference           const_reference;
	typedef typename _Sequence::size_type                 size_type;
	typedef          _Sequence                            container_type;

protected:
	//  See queue::c for notes on these names.
	_Sequence  c;
	_Compare   comp;

public:

	explicit
		priority_queue(const _Compare& __x = _Compare(),
			const _Sequence& __s = _Sequence())
		: c(__s), comp(__x)
	{
		std::make_heap(c.begin(), c.end(), comp);
	}

	template<typename _InputIterator>
	priority_queue(_InputIterator __first, _InputIterator __last,
		const _Compare& __x = _Compare(),
		const _Sequence& __s = _Sequence())
		: c(__s), comp(__x)
	{
		__glibcxx_requires_valid_range(__first, __last);
		c.insert(c.end(), __first, __last);
		std::make_heap(c.begin(), c.end(), comp);
	}


	/**
	*  Returns true if the %queue is empty.
	*/
	bool
		empty() const
	{
		return c.empty();
	}

	/**  Returns the number of elements in the %queue.  */
	size_type
		size() const
	{
		return c.size();
	}

	/**
	*  Returns a read-only (constant) reference to the data at the first
	*  element of the %queue.
	*/
	const_reference
		top() const
	{
		__glibcxx_requires_nonempty();
		return c.front();
	}

	void
		push(const value_type& __x)
	{
		c.push_back(__x);
		std::push_heap(c.begin(), c.end(), comp);
	}

	void
		pop()
	{
		__glibcxx_requires_nonempty();
		std::pop_heap(c.begin(), c.end(), comp);
		c.pop_back();
	}

};

 

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页