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使用了模板默認參數
	
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章