c++的vector容器的简单实现

大耗子 2020年02月24日 329次浏览

c的vector容器的简单实现,直接复刻的c自带的vector容器

#pragma once
#include <windows.h>
template<class T>
class Myvector
{
	T *pBuff;
	size_t num;
	size_t maxNum;
public:
	struct MyIterator
	{
		T *pIt;
		MyIterator& operator=(MyIterator const& srcIt)
		{
			pIt = srcIt.pIt;
			return *this;
		}
		MyIterator& operator+=(int n)
		{
			pIt = pIt + n;
			return *this;
		}
		MyIterator& operator-=(int n)
		{
			pIt = pIt - n;
			return *this;
		}
		T operator *()
		{
			return *pIt;
		}
		MyIterator operator+(int n)
		{
			MyIterator it;
			it.pIt =pIt;
			it.pIt +=  n;
			return it;
		}
		MyIterator operator-(int n)
		{
			MyIterator it;
			it.pIt = pIt;
			it.pIt -= n;
			return it;
		}
		MyIterator& operator++()
		{
			pIt += 1;
			return *this;
		}
		MyIterator operator++(int)
		{
			MyIterator temp(*this);
			pIt += 1;
			return temp;

		}
		bool operator !=(MyIterator const& srcIt) const
		{
			return pIt != srcIt.pIt;
		}
		int operator -(MyIterator const& srcIt)const
		{
			return pIt - srcIt.pIt;
		}
		T& operator[](int index) const
		{
			return pBuff[index];
		}
	};
public:
	MyIterator begin()
	{
		MyIterator it;
		it.pIt = pBuff;
		return it;
	}
	MyIterator end()
	{
		MyIterator it;
		it.pIt = pBuff+num;
		return it;
	}
public:
	Myvector();
	Myvector(int n);
	Myvector(Myvector const& other);
	Myvector(int n,T const& srcDate);
	Myvector(MyIterator b, MyIterator e);
	~Myvector();
public:
	size_t size() const;
	size_t max_size() const;
	size_t capacity() const;
	void reserve(int n);
	bool empty() const;
	void clear();
public:
	bool operator ==(Myvector const & srcVector) const;
	bool operator !=(Myvector const & srcVector) const;
	bool operator >=(Myvector const & srcVector) const;
	bool operator <=(Myvector const & srcVector) const;
	bool operator >(Myvector const & srcVector) const;
	bool operator <(Myvector const & srcVector) const;
	void operator =(Myvector const & srcVector);
	//void operator =(T n);
	T& operator [](int index) const;
public:
	void swap(Myvector vector1);
public:
	void assign(int n,T const& srcdate);//拷贝n个数据
	void assign(MyIterator b,MyIterator e);//拷贝n个数据
public:
	T at(int index)const;
public:
	void push_back(T const & srcDate);//压入数据
	void pop_back();//删除最后一个数据
	void resize(size_t n);
public:
	T front()const;
	T back()const;
private:
	//容器变大重分配
	void Mymalloc()
	{
		if (num>maxNum)
		{
			maxNum = maxNum + ((maxNum >> 1)>1 ? (maxNum >> 1) : 1);
			T* temppBuff = new T[maxNum];
			memcpy(temppBuff, pBuff, sizeof(T)*num);
			if (pBuff)
				delete[]pBuff;
			pBuff = temppBuff;
		}
	}
public:
	MyIterator erase(MyIterator const &beg, MyIterator const &end)
	{
		int be = beg.pIt - pBuff, en = end.pIt - pBuff;
		for (int i = be,j=en; i < num-en+be+1; i++,j++)
			pBuff[i] = pBuff[j];
		num =num-en+be;
		MyIterator tempIt;
		tempIt.pIt = pBuff + be;
		return tempIt;
	}
	MyIterator erase(MyIterator const& pos)
	{
		int index = pos.pIt - pBuff;
		for (int i = index; i < num; i++)
			pBuff[i] = pBuff[i + 1];
		num--;
		MyIterator tempIt;
		tempIt.pIt = pBuff+index;
		return tempIt;
	}
	void insert(MyIterator const& pos, int n, T const& srcDate)
	{
		int index = pos.pIt - pBuff;
		if (num + n > maxNum)
		{
			maxNum = maxNum + n;
			T* temppBuff = new T[maxNum];
			memcpy(temppBuff, pBuff, sizeof(T)*num);
			if (pBuff)
				delete[]pBuff;
			pBuff = temppBuff;
		}
		printf("%d %d %d\n",index, num + n - 1, index + n);
		for (int i = num + n - 1, j = num-1; i >= n + index - 1; i--, j--)
			pBuff[i] = pBuff[j];
		for (int i = index; i < index + n; i++)
			pBuff[i] = srcDate;
		num = num + n;
	}
	void insert(MyIterator const& pos, MyIterator const& beg, MyIterator const& end)
	{
		MyIterator temp = beg;
		int index = pos.pIt - pBuff;
		int n = end.pIt - beg.pIt;
		if (num+n > maxNum)
		{
			maxNum = maxNum +n;
			T* temppBuff = new T[maxNum];
			memcpy(temppBuff, pBuff, sizeof(T)*num);
			if (pBuff)
				delete[]pBuff;
			pBuff = temppBuff;
		}
		for (int i = num + n - 1, j = num - 1; i >= n + index - 1; i--, j--)
		{
			pBuff[i] = pBuff[j];
		}
		for (int i = index; i < index+n; i++)
		{
			pBuff[i] = *(temp);
			temp = temp + 1;
		}
		num = num + n;
	}
	void insert(MyIterator const& pos, T const& srcDate)
	{
		int index = pos.pIt - pBuff;
		if (num>=maxNum)
		{
			maxNum = maxNum + ((maxNum >> 1)>1 ? (maxNum >> 1) : 1);
			T* temppBuff = new T[maxNum];
			memcpy(temppBuff, pBuff, sizeof(T)*num);
			if (pBuff)
				delete[]pBuff;
			pBuff = temppBuff;
		}
		for ( int i = num; i >index; i--)
		{
			pBuff[i] = pBuff[i - 1];
		}
		pBuff[index] = srcDate;
		num++;
	}
};

template<class T>
T Myvector<T>::front()const
{
	return pBuff[0];
}
template<class T>
T Myvector<T>::back()const
{
	return pBuff[num-1];
}


template<class T>
void Myvector<T>::resize(size_t n)
{
	if (n!=num)
	{
		int tempNum = n < num ? n : num;
		T *temppBuff = new T[n];
		memcpy(temppBuff, pBuff, sizeof(T)*tempNum);
		if (pBuff)
			delete[]pBuff;
		pBuff = temppBuff;
		num =maxNum= n;
	}
	
}
template<class T>
void Myvector<T>::clear()
{
	memcpy(pBuff,0 , sizeof(T)*num);
	num =  0;
}

template<class T>
void Myvector<T>::pop_back(){
	if (num)
		num--;
}

template<class T>
void Myvector<T>::push_back(T const & srcDate)
{
	if (num>=maxNum)
	{
		maxNum = maxNum + ((maxNum >> 1)>1 ? (maxNum >> 1) : 1);
		T* temppBuff = new T[maxNum];
		memcpy(temppBuff, pBuff, sizeof(T)*num);
		if (pBuff)
			delete[]pBuff;
		pBuff = temppBuff;
	}
	pBuff[num++] = srcDate;
}

template<class T>
T& Myvector<T>::operator [](int index) const
{
	return pBuff[index];
}

template<class T>
T Myvector<T>::at(int index)const{
	if (index < 0 || index >= num)
		throw "数组越界";
	return pBuff[index];
}

template<class T>
void Myvector<T>::assign(MyIterator b, MyIterator e)
{
	int n = e - b;
	if (pBuff)
	{
		delete[]pBuff;
		if (maxNum < n)
			maxNum = n;
	}
	else
		maxNum = n;
	pBuff = new T[maxNum];
	num = n;
	for (int i = 0; i < num; i++)
		pBuff[i] = *(b+i);
}
template<class T>
void Myvector<T>::assign(int n, T const& srcdate)
{
	if (pBuff)
	{
		delete[]pBuff;
		if (maxNum < n)
			max = n;
	}
	else
		max = n;
	pBuff = new T[maxNum];
	num = n;
	for (int i = 0; i < num; i++)
		pBuff[i] = srcdate;
}

template<class T>
void Myvector<T>::swap(Myvector swapVector)
{
	size_t tempNum = num;
	size_t tempMaxNum = maxNum;
	T *temppBuff = pBuff;

	num = swapVector.num;
	maxNum = swapVector.maxNum;
	pBuff = swapVector.pBuff;

	swapVector.num=tempnum;
	swapVector.maxNum = tempMaxNum;
	swapVector.pBuff = temppBuff;
}

template<class T>
bool Myvector<T>::operator !=(Myvector const & srcVector) const
{
	return !(*this == srcVector);
}

template<class T>
void Myvector<T>::operator =(Myvector const & srcVector)
{
	num = srcVector.num;
	maxNum = srcVector.maxNum;
	if (num)
		pBuff = srcVector.pBuff;
}

template<class T>
bool Myvector<T>::operator >=(Myvector const & srcVector) const
{
	size_t tempNum = num > srcVector.num ? num : srcVector.num;
	for (size_t i = 0; i < tempNum; i++)
	{
		if (pBuff[i] < srcVector.pBuff[i])
			return false;
	}
	return true;
}
template<class T>
bool Myvector<T>::operator >(Myvector const & srcVector) const
{
	size_t tempNum = num > srcVector.num ? num : srcVector.num;
	for (size_t i = 0; i < tempNum; i++)
	{
		if (pBuff[i] <= srcVector.pBuff[i])
			return false;
	}
	return true;
}
template<class T>
bool Myvector<T>::operator <=(Myvector const & srcVector) const
{
	size_t tempNum = num > srcVector.num ? num : srcVector.num;
	for (size_t i = 0; i < tempNum; i++)
	{
		if (pBuff[i] > srcVector.pBuff[i])
			return false;
	}
	return true;
}
template<class T>
bool Myvector<T>::operator <(Myvector const & srcVector) const
{
	size_t tempNum = num > srcVector.num ? num : srcVector.num;
	for (size_t i = 0; i < tempNum; i++)
	{
		if (pBuff[i] >= srcVector.pBuff[i])
			return false;
	}
	return true;
}
//template<class T>
//bool Myvector<T>::operator >(Myvector const & srcVector) const
//{
//	return !(*this <= srcVector);
//}
//template<class T>
//bool Myvector<T>::operator <(Myvector const & srcVector) const
//{
//	return !(*this >= srcVector);
//}

template<class T>
bool Myvector<T>::operator ==(Myvector const & srcVector) const
{
	if (num==servecor.num)
	{
		for (size_t i = 0; i < num; i++)
		{
			if (pBuff[i]!=srcVector.pBuff[i])
				retrun false;
		}
		return true;
	}
	return false;
}
template<class T>
void Myvector<T>::reserve(int n)
{
	num = 0;
	maxNum = n;
	pBuff = new T[maxNum];
	memset(pBuff,0,sizeof(T)*maxNum);
}
template <class T>
size_t Myvector<T>::max_size() const
{
	return INT_MAX / sizeof(T);
}
template <class T>
size_t Myvector<T>::capacity() const
{
	return maxNum;
}
template<class T>
size_t Myvector<T>::size() const
{
	return num;
}
template<class T>
bool Myvector<T>::empty() const
{
	return num == 0;
}
template<class T>
Myvector<T>::Myvector(int n, T const& srcDate)
{
	num = maxNum = n;
	pBuff = NULL;
	if (num)
	{
		pBuff = new T[num];
		for (int i = 0; i < n; i++)
		{
			pBuff[i] = srcDate;
		}
	}
}

template<class T>
Myvector<T>::~Myvector()
{
	if (pBuff)
		delete[]pBuff;
}

template<class T>
Myvector<T>::Myvector(MyIterator b, MyIterator e)
{
	int n=0;
	if ((n=e-b)>0)
	{
		num = maxNum = n;
		pBuff = new T[num];
		for (int i = 0; i < num; i++)
			pBuff[i] = *(b + i);
	}
}

template<class T>
Myvector<T>::Myvector(Myvector const& other)
{
	num = other.num;
	maxNum = other.maxNum;
	if (num)
	{
		pBuff = new T[num];
		memcpy(pBuff,other.pBuff,sizeof(T)*num);
	}
}

template<class T>
Myvector<T>::Myvector(int n)
{
	num = n;
	maxNum = n;
	pBuff = new T[num];
	memset(pBuff,0,sizeof(T)*num);
}


template<class T>
Myvector<T>::Myvector()
{
	pBuff = NULL;
	num = 0;
	maxNum = 0;
}