STL源码剖析——stack

 

stack容器配接器

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


    由于源码比较短,同时是基于其他容器进行操作的,这里只给出源码的剖析:

stl_stack.h
// Filename:    stl_stack.h


#ifndef __SGI_STL_INTERNAL_STACK_H
#define __SGI_STL_INTERNAL_STACK_H

__STL_BEGIN_NAMESPACE

// 如果编译器不能根据前面模板参数推导出后面使用的默认参数类型,
// 那么就需要手工指定, 本实作stack内部容器默认使用deque
// 选用deque可以在存储空间不足时可以动态增加, 而且代价很低
#ifndef __STL_LIMITED_DEFAULT_TEMPLATES
template <class T, class Sequence = deque<T> >
#else
template <class T, class Sequence>
#endif
class stack
{
  // 特化的全局运算符, 提供operator==和<重载则构建出所有运算符
  // 其具体细节见<stl_pair.h>中的说明
  friend bool operator== __STL_NULL_TMPL_ARGS (const stack&, const stack&);
  friend bool operator< __STL_NULL_TMPL_ARGS (const stack&, const stack&);

public:
  // 由于stack仅支持对栈顶元素的操作, 所以不定义STL要求的
  // pointer, iterator, difference_type
  typedef typename Sequence::value_type value_type;
  typedef typename Sequence::size_type size_type;
  typedef typename Sequence::reference reference;
  typedef typename Sequence::const_reference const_reference;

protected:
  Sequence c;   // 这个是我们实际维护的容器

public:
  // 下面的操作完全使用内部容器的成员函数实现
  // 这再次体现了STL高度的可复用性:-)

  // 判断stack是否为空
  bool empty() const { return c.empty(); }

  // stack中元素个数
  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); }

  // 移除栈顶元素, 注意不返回元素的引用,
  // 很多初学者随机用此容器时经常误认为pop()操作同时会返回栈顶元素的引用
  void pop() { c.pop_back(); }
};

// 判断两个stack是否相等, 就要测试其内部维护容器是否相等
// x.c == y.c会调用容器重载的operator ==
template <class T, class Sequence>
bool operator==(const stack<T, Sequence>& x, const stack<T, Sequence>& y)
{
  return x.c == y.c;
}

template <class T, class Sequence>
bool operator<(const stack<T, Sequence>& x, const stack<T, Sequence>& y)
{
  return x.c < y.c;
}

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

Output:
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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值