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;
};
相关推荐
blasit2 天前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
肆忆_3 天前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++
不想写代码的星星3 天前
虚函数表:C++ 多态背后的那个男人
c++
端平入洛5 天前
delete又未完全delete
c++
端平入洛5 天前
auto有时不auto
c++
郑州光合科技余经理6 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1236 天前
matlab画图工具
开发语言·matlab
dustcell.6 天前
haproxy七层代理
java·开发语言·前端
norlan_jame6 天前
C-PHY与D-PHY差异
c语言·开发语言
哇哈哈20216 天前
信号量和信号
linux·c++