容器适配器实现

template<class T, class Container = deque<T>>
class Stack {
public:
	void push(const T& val) {
		_c.push_back(val);
	}

	void pop() {
		_c.pop_back();
	}

	const T& top() {
		return _c.back();
	}

	size_t size() const {
		return _c.size();
	}

	bool empty() const {
		return _c.empty();
	}

private:
	Container _c;
};


void testst() {
	Stack<int> st;
	//Stack<int, vector<int>> st;
	//Stack<int, list<int>> st;

	st.push(1);
	st.push(2);
	st.push(3);
	st.push(4);

	while (!st.empty()) {
		cout << st.top() << " ";
		st.pop();
	}
	cout << endl;
}

template<class T, class Container = deque<T>>
class Queue {
public:
	void push(const T& val) {
		_c.push_back(val);
	}

	void pop() {
		_c.pop_front();
	}

	T& front() {
		return _c.front();
	}

	T& back() {
		return _c.back();
	}

	size_t size() const {
		return _c.size();
	}

	bool empty() const {
		return _c.empty();
	}
private:
	Container _c;
};

void testQ() {
	Queue<int> q;
	//Queue<int, list<int>> q;

	//vector不能实现队列, 因为没有提供头删的接口
	//Queue<int, vector<int>> q;

	q.push(1);
	q.push(2);
	q.push(3);
	q.push(4);
	
	while (!q.empty()) {
		cout << q.front() << " ";
		q.pop();
	}
	cout << endl;
}

class Date {
public:
	Date(int y, int m, int d)
		:_y(y), _m(m), _d(d)
	{ }

	bool operator>(const Date& d) const {
		if (_y > d._y)
			return true;
		else if (_y == d._y) {
			if (_m > d._m)
				return true;
			else if (_m == d._m) {
				if (_d > d._d)
					return true;
			}
		}
		return false;
	}

	bool operator<(const Date& d) const {
		if (_y < d._y)
			return true;
		else if (_y == d._y) {
			if (_m < d._m)
				return true;
			else if (_m == d._m) {
				if (_d < d._d)
					return true;
			}
		}
		return false;
	}
public:
	int _y;
	int _m;
	int _d;
};

ostream& operator<<(ostream& cout, const Date& d)
{
	cout << d._y << "-" << d._m << "-" << d._d << endl;
	return cout;
}

template<class T, class Container = vector<T>, class Compare = Less<T>>
class Priority_Queue {
public:
	void push(const T& val) {
		_c.push_back(val);
		shiftUp(_c.size() - 1);
	}

	void pop() {
		swap(_c[0], _c[_c.size() - 1]);
		_c.pop_back();
		shiftDown(0);
	}

	T& top() {
		return _c.front();
	}

	size_t size() const {
		return _c.size();
	}

	bool empty() const {
		return _c.empty();
	}
private:
	void shiftDown(int parent) {
		int child = parent * 2 + 1;
		while (child < size()) {
			//if (child + 1 < size() && _c[child] < _c[child + 1])
			//通过仿函数实现比较的逻辑
			if (child + 1 < size() && _cmp(_c[child] , _c[child + 1]))
				++child;
			if (_cmp(_c[parent] , _c[child])) {
				swap(_c[parent], _c[child]);
				parent = child;
				child = 2 * parent + 1;
			}
			else
				break;
		}
	}
	
	void shiftUp(int child) {


		int parent = (child - 1) / 2;
		while (child > 0) {
			if (_cmp(_c[parent] , _c[child])) {
				swap(_c[parent], _c[child]);
				child = parent;
				parent = (child - 1) / 2;
			}
			else
				break;			
		}
	}

private:
	Container _c;
	Compare _cmp;
};

template <class T>
struct Greater{
	bool operator()(const T& val1, const T& val2) {
		return val1 > val2;
	}
};

template <class T>
struct Less {
	bool operator()(const T& val1, const T& val2) {
		return val1 < val2;
	}
};

void testPQ() {
	Priority_Queue<int, vector<int>, Less<int>> pq;
	//Priority_Queue<int, deque<int>, Less<int>> pq;
	
	//list不能实现优先级队列, 因为list不支持随机访问
	//Priority_Queue<int, list<int>, Less<int>> pq;

	pq.push(10);
	pq.push(1);
	pq.push(12);
	pq.push(50);
	pq.push(5);
	pq.push(100);

	while (!pq.empty()) {
		cout << pq.top() << " ";
		pq.pop();
	}
	cout << endl;
}

void testPQ2()
{
	Priority_Queue<Date, deque<Date>, Greater<Date>> pq;

	pq.push(Date(2020, 5, 31));
	pq.push(Date(2022, 5, 31));
	pq.push(Date(2008, 5, 31));
	pq.push(Date(2025, 5, 31));
	pq.push(Date(2020, 12, 31));
	pq.push(Date(2020, 5, 3));

	while (!pq.empty())
	{
		cout << pq.top();
		pq.pop();
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

殇&璃

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值