zoukankan      html  css  js  c++  java
  • 从零开始写STL—栈和队列

    从零开始写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);
    	}
    
  • 相关阅读:
    Linux添加用户组和删除用户组
    购物意图分析
    架构是什么来的
    如何突破浏览器加载并发数的限制
    写JS自执行函数时要注意的
    网页是什么
    JVM
    javascript的边界
    浏览器
    HTTP
  • 原文地址:https://www.cnblogs.com/joeylee97/p/8662947.html
Copyright © 2011-2022 走看看