queue的模拟实现

 queue.h

#include<vector>
#include<list>

namespace Ljw 
{
	template<class T,class Container=vector<int>>
	class queue//默认list的
	{
	public:
		T& front()
		{
			return _con.front();
		}
		void push(const T&x)
		{
			_con.push_back(x);
		}
		void pop()
		{
			//_con.pop_front();//vecot没有头删,因为代价太大了,要想强制匹配用erase
			_con.erase(_con.begin());
		}
		T& back()
		{
			return _con.back();
		}

	private:
		Container _con;
	};


	void test1()
	{
		queue<int, list<int>> pq;
		pq.push(1);
		pq.push(2);
		pq.push(3);
		pq.pop();
		cout << pq.front() << endl;

		cout << pq.back() << endl;	

		queue<int, vector<int>> pq1;
		pq1.push(1);
		pq1.push(2);
		pq1.push(3);
		pq1.pop();
		cout << pq1.front() << endl;

		cout << pq1.back() << endl;
	}
}

 test.c

#include<iostream>

using namespace std;

#include"queue.h"//必须在下面,因为编译的时候.h只会往上找
int main()
{
	Ljw::test1();
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值