From beb41badf8ea2df430bebcd86e68effaf49e349c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=93=E8=88=AA?= <1210603696@qq.com> Date: Wed, 15 Oct 2014 13:33:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90priority=5Fqueue?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TinySTL/Queue.h | 52 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/TinySTL/Queue.h b/TinySTL/Queue.h index 8a6b782..52a9b5c 100644 --- a/TinySTL/Queue.h +++ b/TinySTL/Queue.h @@ -12,7 +12,57 @@ namespace TinySTL{ //class of priority_queue template , class Compare = TinySTL::less> - class priority_queue{}; + class priority_queue{ + public: + typedef T value_type; + typedef Container container_type; + typedef typename Container::reference reference; + typedef typename Container::const_reference const_reference; + typedef typename Container::size_type size_type; + private: + container_type container_; + Compare compare_; + public: + explicit priority_queue(const Compare& comp = Compare(), + const Container& ctnr = Container()) + : container_(ctnr), compare_(comp){} + template + priority_queue(InputIterator first, InputIterator last, + const Compare& comp = Compare(), + const Container& ctnr = Container()) + : container_(ctnr), compare_(comp){ + container_.insert(container_.end(), first, last); + TinySTL::make_heap(container_.begin(), container_.end()); + } + bool empty() const{ + return container_.empty(); + } + size_type size() const{ + return container_.size(); + } + reference top() { + return container_.front(); + } + void push(const value_type& val){ + container_.push_back(val); + TinySTL::push_heap(container_.begin(), container_.end(), compare_); + } + void pop(){ + TinySTL::pop_heap(container_.begin(), container_.end(), compare_); + container_.pop_back(); + } + void swap(priority_queue& x){ + TinySTL::swap(container_, x.container_); + TinySTL::swap(compare_, x.compare_); + } + public: + template + friend void swap(priority_queue& x, priority_queue& y); + }; + template + void swap(priority_queue& x, priority_queue& y){ + x.swap(y); + } } #endif \ No newline at end of file