vector容器的实现、stack容器适配器的实现

vector容器:

功能:像容器一样存放各种类型的对象,是一个存放任意类型的动态数组,能够增加和压缩数据;

            vector是一个类模板而不是一种数据类型,故对它的定义,需要指定类型;

优点:当程序员无法知道自己需要的数组的规模多大时,用其来解决问题可以达到最大节约空间的目的。

代码实现:

​//Vector容器实现(不定长顺序表)
template <typename _Ty>
class Vector
{
public:
	Vector()
	{
		parr =new _Ty[2]();
		cursize = 0;
		totalsize = 2;
	}
	~Vector()
	{
		delete [] parr;
		parr = NULL;
	}
	void push_back(const _Ty& val)  //尾插
	{
		insert(cursize,val);
		/*if(full())
		{
			resize();
		}
		parr[cursize++] = val;*/
	}
	void insert(int pos,const _Ty& val)  //任意位置插入
	{
		if(pos < 0 || pos > cursize)
		{
			throw std::exception("pos is error!");
		}
		if(full())
		{
			resize();  //扩容
		}
		int index = cursize;
		for(index;index>pos;index--)
		{
			parr[index] = parr[index - 1];
		}
		parr[pos] = val;
		cursize++;
	}
	void pop_back()  //尾删
	{
		erase(cursize-1);
	}
	void erase(int pos)  //删除任意位置
	{
		if(pos < 0 || pos >= totalsize)
		{
			throw std::exception("pos is error!");
		}
		if(empty())
		{
			throw std::exception("Vector is NULL!");
		}
		int index = pos;
		for(index;index<cursize -1;index++)
		{
			parr[index] = parr[index+1];
		}
		cursize--;
	}
	_Ty back()   //获取最后位置元素
	{
		if(empty())
		{
			throw std::exception("Vector is NULL!");
		}
		return parr[cursize-1];
	}
	void show()  //打印
	{
		int index = 0;
		for(index;index<cursize ;index++)
		{
			std::cout<<parr[index]<<" ";
		}
		std::cout<<std::endl;
	}
	bool empty()   //判空
	{
		return cursize == 0;
	}
	
private:
	bool full()  //判满
	{
		return cursize == totalsize;
	}
	void resize()  //扩容
	{
		_Ty * pnewarr =new _Ty[totalsize*2]();
		memcpy(pnewarr,parr,sizeof(_Ty)*totalsize);
		delete [] parr;
		parr = pnewarr;
		totalsize *=2;
	}
	_Ty* parr;
	int cursize;  //有效数据个数,可插入数据
	int totalsize;   //总空间大小
};

//主函数
int main()
{
	Vector<int > Elem;
	for(int i = 0;i<10;i++)
	{
		Elem.push_back (i);
	}
	Elem.show();

	return 0;
}​

stack容器适配器:

stack是一个容器适配器,默认的stack是基于deque实现的,但是可以显式的让其用vectorlist实现。

stack头文件中:

stack容器适配器基于默认为vector容器的代码实现:

#include <iostream>

//Vector容器实现(不定长顺序表)
template <typename _Ty>
class Vector
{
public:
	Vector()
	{
		parr =new _Ty[2]();
		cursize = 0;
		totalsize = 2;
	}
	~Vector()
	{
		delete [] parr;
		parr = NULL;
	}
	void push_back(const _Ty& val)
	{
		insert(cursize,val);
		/*if(full())
		{
			resize();
		}
		parr[cursize++] = val;*/
	}
	void insert(int pos,const _Ty& val)
	{
		if(pos < 0 || pos > cursize)
		{
			throw std::exception("pos is error!");
		}
		if(full())
		{
			resize();  //扩容
		}
		int index = cursize;
		for(index;index>pos;index--)
		{
			parr[index] = parr[index - 1];
		}
		parr[pos] = val;
		cursize++;
	}
	void pop_back()
	{
		erase(cursize-1);
	}
	void erase(int pos)
	{
		if(pos < 0 || pos >= totalsize)
		{
			throw std::exception("pos is error!");
		}
		if(empty())
		{
			throw std::exception("Vector is NULL!");
		}
		int index = pos;
		for(index;index<cursize -1;index++)
		{
			parr[index] = parr[index+1];
		}
		cursize--;
	}
	_Ty back()   //获取最后位置元素
	{
		if(empty())
		{
			throw std::exception("Vector is NULL!");
		}
		return parr[cursize-1];
	}
	void show()
	{
		int index = 0;
		for(index;index<cursize ;index++)
		{
			std::cout<<parr[index]<<" ";
		}
		std::cout<<std::endl;
	}
	bool empty()
	{
		return cursize == 0;
	}
	
private:
	bool full()  //判满
	{
		return cursize == totalsize;
	}
	void resize()  //扩容
	{
		_Ty * pnewarr =new _Ty[totalsize*2]();
		memcpy(pnewarr,parr,sizeof(_Ty)*totalsize);
		delete [] parr;
		parr = pnewarr;
		totalsize *=2;
	}
	_Ty* parr;
	int cursize;  //有效数据个数,可插入数据
	int totalsize;   //总空间大小
};

//Stack容器适配器基于Vector容器实现类模板
template <typename _Ty,
					template <typename T>	
					class Container = Vector>
class Stack
{
public:
	Stack(){}
	~Stack(){}
	void push(const _Ty& val)
	{
		c.push_back(val);
	}
	void pop()
	{
		c.pop_back();
	}
	_Ty top()
	{
		return c.back();
	}
	bool empty()
	{
		return c.empty();
	}
private:
	Container<_Ty> c;   //相当于Vector<_Ty> c;
};
int main()
{
	Stack<int> s1;
	Stack<int ,Vector> s2;  //s1,s2同种方式生成,s1使用了模板默认参数
	
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值