您的位置:首页 > 产品设计 > UI/UE

STL源码—stack,queue

2014-11-17 18:09 323 查看
stack:

stack是一种“先进后出”的数据结构,它只能在栈顶对数据进行操作,即只能在栈顶进行新增元素、移除元素、取得最顶端元素。不能进行遍历行为,所以不需要设计自己的迭代器。在SGI
STL的源码<stl_stack.h>的设计中,它是基于某种容器作为底部结构的,默认容器是deque容器,因为deque是双向开口的数据结构。封闭其头端开口就能够得到stack。用户也可以自己指定容器的类型。

stack是没有迭代器的

在创建stack的时候需要指定底层容器的类型

stack可以由deque和list,vector作为底层结构

#ifndef __SGI_STL_INTERNAL_STACK_H
#define __SGI_STL_INTERNAL_STACK_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 stack;

template <class _Tp, class _Seq>
bool operator==(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y);

template <class _Tp, class _Seq>
bool operator<(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y);

template <class _Tp, class _Sequence>
class stack {

  // requirements:

  __STL_CLASS_REQUIRES(_Tp, _Assignable);
  __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 stack<_Tp1, _Seq1>&,
                          const stack<_Tp1, _Seq1>&);
  template <class _Tp1, class _Seq1>
  friend bool operator< (const stack<_Tp1, _Seq1>&,
                         const stack<_Tp1, _Seq1>&);
#else /* __STL_MEMBER_TEMPLATES */
  friend bool __STD_QUALIFIER
  operator== __STL_NULL_TMPL_ARGS (const stack&, const stack&);
  friend bool __STD_QUALIFIER
  operator< __STL_NULL_TMPL_ARGS (const stack&, const stack&);
#endif /* __STL_MEMBER_TEMPLATES */

public:
	// 由于stack仅支持对栈顶元素的操作, 所以不定义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:
	//下面对stack的维护完全依赖于底层容器的操作
  stack() : c() {}
  explicit stack(const _Sequence& __s) : c(__s) {}

  //判断容器是否为空
  bool empty() const { return c.empty(); }
  //获取容器的大小,即容器中元素的个数
  size_type size() const { return c.size(); }
  //返回栈顶元素的引用
  reference top() { return c.back(); }
  const_reference top() const { return c.back(); }
  //在栈顶追加元素
  void push(const value_type& __x) { c.push_back(__x); }
  //弹出栈顶的元素,但不返回任何内容
  void pop() { c.pop_back(); }
};

//下面是依赖于底层容器的操作运算符
template <class _Tp, class _Seq>
bool operator==(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y)
{
  return __x.c == __y.c;
}

template <class _Tp, class _Seq>
bool operator<(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y)
{
  return __x.c < __y.c;
}

#ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER

template <class _Tp, class _Seq>
bool operator!=(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y)
{
  return !(__x == __y);
}

template <class _Tp, class _Seq>
bool operator>(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y)
{
  return __y < __x;
}

template <class _Tp, class _Seq>
bool operator<=(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y)
{
  return !(__y < __x);
}

template <class _Tp, class _Seq>
bool operator>=(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y)
{
  return !(__x < __y);
}

#endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */

__STL_END_NAMESPACE

#endif /* __SGI_STL_INTERNAL_STACK_H */

// Local Variables:
// mode:C++
// End:


一个对应的测试程序:

// constructing stacks  
#include <iostream>       // std::cout  
#include <stack>          // std::stack  
#include <vector>         // std::vector  
#include <deque>          // std::deque  

int main ()  
{  
	std::deque<int> mydeque (3,100);          // deque with 3 elements  
	std::vector<int> myvector (2,200);        // vector with 2 elements  

	std::stack<int> first;                    // empty stack  
	std::stack<int> second (mydeque);         // stack initialized to copy of deque  

	std::stack<int,std::vector<int> > third;  // empty stack using vector  
	std::stack<int,std::vector<int> > fourth (myvector);  

	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';  
	second.push(2);  
	std::cout << "The element at the top of stack second is: "  
		<< second.top( ) << "." << std::endl;  
	std::cout << "size of second: " << second.size() << '\n';  

	return 0;  
}
输出:

size of first: 0

size of second: 3

size of third: 0

size of fourth: 2

The element at the top of stack second is: 2.

size of second: 4

请按任意键继续. . .

queue:

queue是一种“先进先出”的数据结构,可以对两端进行操作,但是只能在队列头部进行移除元素,只能在队列尾部新增元素,可以访问队列尾部和头部的元素,但是不能遍历容器,所以queue不需要设计自己的容器。在SGI
STL的源码<stl_queue.h>的class
queue设计中,它是基于某种容器作为底部结构的,默认容器是deque容器,用户也可以自己指定容器的类型。

queue可以以deque和list作为底层的容器

#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 */
一个实现的例子:

#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;
}


输出结果:

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

请按任意键继续. . .
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: