From 78a80ff758a9bc51a959456c13eb0069aec0575a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=93=E8=88=AA?= <1210603696@qq.com> Date: Thu, 16 Oct 2014 17:16:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90stack?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TinySTL/Stack.h | 67 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 64 insertions(+), 3 deletions(-) diff --git a/TinySTL/Stack.h b/TinySTL/Stack.h index 560756a..d77a8b2 100644 --- a/TinySTL/Stack.h +++ b/TinySTL/Stack.h @@ -1,11 +1,72 @@ #ifndef _STACK_H_ #define _STACK_H_ -#include "Deque.h" +#include "Vector.h" namespace TinySTL{ //class of stack - template> - class stack{}; + template> + class stack{ + public: + typedef typename Container::value_type value_type; + typedef typename Container::reference reference; + typedef typename Container::size_type size_type; + typedef Container container_type; + private: + container_type container_; + public: + explicit stack(const container_type& ctnr = container_type()) :container_(ctnr){} + + bool empty() const{ return container_.empty(); } + size_type size() const{ return container_.size(); } + value_type& top(){ return (container_.back()); } + const value_type& top() const{ return (container_.back()); } + void push(const value_type& val){ container_.push_back(val); } + void pop(){ container_.pop_back(); } + void swap(stack& x){ TinySTL::swap(container_, x.container_); } + public: + template + friend bool operator== (const stack& lhs, const stack& rhs); + template + friend bool operator!= (const stack& lhs, const stack& rhs); + template + friend bool operator< (const stack& lhs, const stack& rhs); + template + friend bool operator<= (const stack& lhs, const stack& rhs); + template + friend bool operator> (const stack& lhs, const stack& rhs); + template + friend bool operator>= (const stack& lhs, const stack& rhs); + template + friend void swap(stack& x, stack& y); + }; + template + bool operator== (const stack& lhs, const stack& rhs){ + return lhs.container_ == rhs.container_; + } + template + bool operator!= (const stack& lhs, const stack& rhs){ + return lhs.container_ != rhs.container_; + } + template + bool operator< (const stack& lhs, const stack& rhs){ + return lhs.container_ < rhs.container_; + } + template + bool operator<= (const stack& lhs, const stack& rhs){ + return lhs.container_ <= rhs.container_; + } + template + bool operator>(const stack& lhs, const stack& rhs){ + return lhs.container_ > rhs.container_; + } + template + bool operator>= (const stack& lhs, const stack& rhs){ + return lhs.container_ >= rhs.container_; + } + template + void swap(stack& x, stack& y){ + x.swap(y); + } } #endif \ No newline at end of file