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;
};
相关推荐
居然是阿宋2 分钟前
Kotlin高阶函数 vs Lambda表达式:关键区别与协作关系
android·开发语言·kotlin
ChoSeitaku12 分钟前
17.QT-Qt窗口-工具栏|状态栏|浮动窗口|设置停靠位置|设置浮动属性|设置移动属性|拉伸系数|添加控件(C++)
c++·qt·命令模式
Cao12345678932137 分钟前
简易学生成绩管理系统(C语言)
c语言·开发语言
The Future is mine38 分钟前
C# new Bitmap(32043, 32043, PixelFormat.Format32bppArgb)报错:参数无效,如何将图像分块化处理?
开发语言·c#
亿坊电商41 分钟前
PHP框架在微服务迁移中能发挥什么作用?
开发语言·微服务·php
烁34741 分钟前
每日一题(小白)模拟娱乐篇33
java·开发语言·算法
坐吃山猪1 小时前
Python-Agent调用多个Server-FastAPI版本
开发语言·python·fastapi
88号技师1 小时前
【1区SCI】Fusion entropy融合熵,多尺度,复合多尺度、时移多尺度、层次 + 故障识别、诊断-matlab代码
开发语言·机器学习·matlab·时序分析·故障诊断·信息熵·特征提取
软行1 小时前
LeetCode 每日一题 2845. 统计趣味子数组的数目
数据结构·c++·算法·leetcode
北漂老男孩1 小时前
Java对象转换的多种实现方式
java·开发语言