欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

tinystl实现(第十六步:queue实现)

程序员文章站 2022-05-24 19:10:32
...

经过长时间的学习终于可以开始tinystl的仿(chao)写工作了,本文参考了这位大神的github,坦白讲我只是补充了注释,因为tinystl的代码真的非常经典而我又没什么这种大型项目的经验,所以只能这样做,不过相信能够有助于大家的学习
本文的queue包括队列和优先队列,前者由deque封装实现,后者由vector和堆共同实习,展现了强大的代码复用能力
#强烈建议按顺序阅读本专栏
queue.h

#pragma once
#ifndef _QUEUE_H_
#define _QUEUE_H_

#include "Deque.h"
#include "Functional.h"
#include "Vector.h"

namespace mySTL {
	//class of queue
	template<class T, class Container = mySTL::deque<T>>
	//container用deque的,其他的所有函数都可以通过deque实现!
	class 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 container_;
	public:
		//功能全部通过container调用deque
		queue() {}
		explicit queue(const container_type& ctnr) :container_(ctnr) {}

		bool empty() const { return container_.empty(); }
		size_type size() const { return container_.size(); }
		reference& front() { return container_.front(); }
		const_reference& front() const { return container_.front(); }
		reference& back() { return container_.back(); }
		const_reference& back() const { return container_.back(); }
		void push(const value_type& val) { container_.push_back(val); }
		void pop() { container_.pop_front(); }
		void swap(queue& x) { container_.swap(x.container_); }
	public:
		template <class T, class Container>
		friend bool operator== (const queue<T, Container>& lhs, const queue<T, Container>& rhs);
		template <class T, class Container>
		friend bool operator!= (const queue<T, Container>& lhs, const queue<T, Container>& rhs);
		template <class T, class Container>
		friend void swap(queue<T, Container>& x, queue<T, Container>& y);
	};
	template <class T, class Container>
	bool operator== (const queue<T, Container>& lhs, const queue<T, Container>& rhs) {
		return lhs.container_ == rhs.container_;
	}
	template <class T, class Container>
	bool operator!= (const queue<T, Container>& lhs, const queue<T, Container>& rhs) {
		return lhs.container_ != rhs.container_;
	}
	template <class T, class Container>
	void swap(queue<T, Container>& x, queue<T, Container>& y) {
		mySTL::swap(x.container_, y.container_);
	}
	//优先队列使用vector做container
	template <class T, class Container = mySTL::vector<T>,
		class Compare = mySTL::less<typename Container::value_type>>
		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 <class InputIterator>
			priority_queue(InputIterator first, InputIterator last,
				const Compare& comp = Compare(),
				const Container& ctnr = Container())
				: container_(ctnr), compare_(comp) {
				container_.insert(container_.end(), first, last);
				mySTL::make_heap(container_.begin(), container_.end());//插在vector的最后然后建堆
			}
			//以下全部调用vector实现
			bool empty() const {
				return container_.empty();
			}
			size_type size() const {
				return container_.size();
			}
			reference top() {
				return container_.front();
			}
			//添加先调用vector的insert然后再调用堆
			void push(const value_type& val) {
				container_.push_back(val);
				mySTL::push_heap(container_.begin(), container_.end(), compare_);
			}
			//添加先调用堆再用vector排除
			void pop() {
				mySTL::pop_heap(container_.begin(), container_.end(), compare_);
				container_.pop_back();
			}
			void swap(priority_queue& x) {
				mySTL::swap(container_, x.container_);
				mySTL::swap(compare_, x.compare_);
			}
		public:
			template <class T, class Container, class Compare>
			friend void swap(priority_queue<T, Container, Compare>& x, priority_queue<T, Container, Compare>& y);
	};
	template <class T, class Container, class Compare>
	void swap(priority_queue<T, Container, Compare>& x, priority_queue<T, Container, Compare>& y) {
		x.swap(y);
	}
}

#endif
相关标签: tinystl stl c++