stack和queue

文章目录

适配器模式

适配器是一种设计模式(设计模式是一套被反复使用的、多数人知晓的、经过分类编目的、代码设计经验的总结),该种模式是将一个类的接口转换成客户希望的另外一个接口

stack的模拟实现

cpp 复制代码
template<class T, class Container = vector<T>>
class stack
{
	Container _con;
public:
	stack() {}
	~stack() {}
	void push(const T& x) { _con.push_back(x); }
	void pop() { _con.pop_back(); }
	T& top() { return _con.back(); }
	const T& top() const { return _con.back(); }
	size_t size() const { return _con.size(); }
	bool empty() const { return _con.empty(); }
};
void test_stack()
{
	stack<int> st;
	st.push(1);
	st.push(2);
	st.push(3);
	st.push(4);
	while (!st.empty())
	{
		cout << st.top() << " ";
		st.pop();
	}
	cout << endl;
}

queue的模拟实现

cpp 复制代码
template<class T, class Container = deque<T>>
class queue
{
	Container _con;
public:
	queue() {}
	~queue() {}
	bool empty() const { return _con.empty(); }
	size_t size() const { return _con.size(); }
	T& front() { return _con.front(); }
	const T& front() const { return _con.front(); }
	T& back() { return _con.back(); }
	const T& back() const { return _con.back(); }
	void push(const T& x) { _con.push_back(x); }
	void pop() { _con.pop_front(); }
};
void test_queue()
{
	queue<int> q;
	q.push(1);
	q.push(2);
	q.push(3);
	q.push(4);
	while (!q.empty())
	{
		cout << q.front() << ' ';
		q.pop();
	}
	cout << endl;
}

优先级队列

cpp 复制代码
template<class T>
struct less{
	bool operator()(const T& x, const T& y)
	{ return x < y; }
};
template<class T>
struct greater{
	bool operator()(const T& x, const T& y)
	{ return x > y; }
};
template<class T, class Container=vector<T>, class Compare=less<T>>
class priority_queue
{
public:
	priority_queue() {}
	void push(const T& x)
	{
		_con.push_back(x);
		AdjustUp(_con.size() - 1);
	}
	const T& top() const
	{
		return _con.front();
	}
	bool empty() const 
	{
		return _con.empty();
	}
	void pop()
	{
		assert(!_con.empty());
		swap(_con[0], _con[_con.size() - 1]);
		_con.pop_back();
		AdjustDown(0);
	}
private:
	// 向上调整
	void AdjustUp(int child)
	{
		Compare com;	// 仿函数
		int parent = (child - 1) / 2;
		while (child > 0)
		{
			if (com(_con[parent], _con[child]))
			{
				swap(_con[parent], _con[child]);
				child = parent;
				parent = (child - 1) / 2;
			}
			else break;
		}
	}
	// 向下调整
	void AdjustDown(int parent)
	{
		Compare com;	// 仿函数
		int child = 2 * parent + 1;
		while (child < _con.size())
		{
			if (child + 1 < _con.size() && com(_con[child], _con[child+1]))
				child = child + 1;
			if (com(_con[parent], _con[child]))
			{
				swap(_con[parent], _con[child]);
				parent = child;
				child = 2 * parent + 1;
			}
			else break;
		}
	}
	Container _con;
};


			{
				swap(_con[parent], _con[child]);
				parent = child;
				child = 2 * parent + 1;
			}
			else break;
		}
	}
	Container _con;
};
相关推荐
Never_Satisfied20 分钟前
在JavaScript / HTML中,div容器在内容过多时不显示超出的部分
开发语言·javascript·html
艾莉丝努力练剑1 小时前
【C++STL :stack && queue (一) 】STL:stack与queue全解析|深入使用(附高频算法题详解)
linux·开发语言·数据结构·c++·算法
胡萝卜3.01 小时前
深入理解string底层:手写高效字符串类
开发语言·c++·学习·学习笔记·string类·string模拟实现
kyle~1 小时前
计算机系统---CPU的进程与线程处理
linux·服务器·c语言·c++·操作系统·计算机系统
西柚小萌新1 小时前
【Python从入门到精通】--Pycharm增加内存
开发语言·python·pycharm
不爱编程的小九九1 小时前
小九源码-springboot082-java旅游攻略平台
java·开发语言·旅游
只是懒得想了2 小时前
用C++实现一个高效可扩展的行为树(Behavior Tree)框架
java·开发语言·c++·design-patterns
bkspiderx2 小时前
C++设计模式之行为型模式:模板方法模式(Template Method)
c++·设计模式·模板方法模式
我是华为OD~HR~栗栗呀2 小时前
华为OD-23届考研-Java面经
java·c++·后端·python·华为od·华为·面试
yan8626592462 小时前
于 C++ 的虚函数多态 和 模板方法模式 的结合
java·开发语言·算法