从零开始写STL—栈和队列

本文深入探讨了STL中的栈和队列数据结构,详细解析了它们的实现原理和使用方法,包括适配器模式的应用,以及如何基于deque实现这两种数据结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

从零开始写STL—栈和队列

适配器模式

  • 意图:将一个类的接口转换成客户希望的另外一个接口。适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
  • 主要解决:主要解决在软件系统中,常常要将一些"现存的对象"放到新的环境中,而新环境要求的接口是现对象不能满足的。
  • 何时使用: 1、系统需要使用现有的类,而此类的接口不符合系统的需要。 2、想要建立一个可以重复使用的类,用于与一些彼此之间没有太大关联的一些类,包括一些可能在将来引进的类一起工作,这些源类不一定有一致的接口。 3、通过接口转换,将一个类插入另一个类系中。(比如老虎和飞禽,现在多了一个飞虎,在不增加实体的需求下,增加一个适配器,在里面包容一个虎对象,实现飞的接口。)
  • 如何解决:继承或依赖(推荐)。

基于deque的栈

    template<class T,class Sequence = deque<T>>
    class stack
    {
    public:
        typedef T value_type;
        typedef T& reference;
        typedef size_t size_type;
    protected:
        Sequence c;
    public:
        stack() :c() {}
        bool empty()  
        { 
            return c.empty(); 
        }
        size_type size()  
        { 
            return  c.size(); 
        }
        value_type& top()
        {
            if(!empty())
                return *(c.begin());
            else
            {
                std::cerr << "Top on empty Stack!" << std::endl;
                std::exit(1);
            }
        }
        void pop()
        {
            if(!empty())
                c.pop_front();
            else
            {
                std::cerr << "pop on empty Stack!" << std::endl;
                std::exit(1);
            }
        }
        void push(const value_type& x)
        {
            c.push_front(x);
        }
        stack<T> swap(stack<T>& rhs)
        {
            c.swap(rhs.c);
            return *this;
        }
        bool operator==(stack<T> &rhs)
        {
            return c == rhs.c;
        }
        bool operator!=(stack<T>& rhs)
        {
            return c != rhs.c;
        }
    };
    template<typename T>
    void swap(stack<T>& a, stack<T>& b)
    {
        a.swap(b);
    }

基于deque的队列

    template<class T,class Sequence = deque<T>>
    class queue
    {
    public:
        typedef T value_type;
        typedef T& reference;
        typedef size_t size_type;
        typedef typename Sequence::iterator iterator;
    protected:
        Sequence c;
        void check()
        {
            if (empty())
            {
                std::cerr << "Empty !" << std::endl;
                std::exit(1);
            }
        }
    public:
        queue():c(){}
        
        iterator begin()
        {
            return c.begin();
        }
        
        iterator end()
        {
            return c.end();
        }

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


        size_type size()
        {
            return c.size();
        }
        value_type& front()
        {
            check();
            return *(c.begin());
        }
        value_type& back()
        {
            check();
            auto tmp = c.end();
            tmp--;
            return *(tmp);
        }
        const value_type& front() const
        {
            check();
            return *(c.begin());
        }
        const value_type& back() const
        {
            check();
            return *(c.end() - 1);
        }
        void push(const value_type& x)
        {
            c.push_back(x);
        }
        void pop()
        {
            check();
            c.pop_front();
        }
        bool operator==( queue<T>& rhs)
        {
            return c == rhs.c;
        }
        bool operator!=( queue<T>& rhs)
        {
            return !(*this == rhs);
        }
        queue<T>& swap(queue<T>& rhs)
        {
            c.swap(rhs.c);
            return *this;
        }
        
    };
    template<typename T>
    void swap(queue<T> &a, queue<T> &b)
    {
        a.swap(b);
    }

转载于:https://www.cnblogs.com/joeylee97/p/8662947.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值