欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

实现 STL--vector

程序员文章站 2024-02-18 11:07:58
...
#pragma once
#include<iostream>
using namespace std;

template<class T>
class Vector
{
public :
	typedef T* Iterator;
public :
	Vector()
		:_start(0)
		, _end(0)
		, _endOfStorage(0)
	{}
	//顺序表中右size个值为 data元素
	Vector(const T& data, size_t size)

		:_start(new T[size])
		, _end(_start + size)
		, _endOfStorage(_start + size)
	{
		for (int i = 0; i < size; i++)
			_start[i] = data;
	}
	//用一段左开右闭的区间进行初始化
	Vector(const T* first, const T* end)
		:_start(new T[end - first])
		, _end(_start + (end - first))
		, _endOfStorage(_end)
	{
		for (int i = 0; i < (end - first); i++)
			_start[i] = first[i];
	}

	Vector(const Vector<T>& v);
	Vector<T>& operator=(const Vector<T>& v);
		
	~Vector()
	{
		if (_start)
		{
			delete [] _start;
			_start = _end = _endOfStorage = 0;
		}
	}

	/////////////////////////////////////////////////
	////  Tterator
	Iterator Begin()
	{
		return _start;
	}
	Iterator End()
	{
		return _end;
	}
	///////////////////////////////////////////////////
	///Capatipy
	size_t Size()const
	{
		return _end - _start;
	}
	size_t Capacity()const
	{
		return _endOfStorage - _start;
	}

	bool Empty() const
	{
		return _start == _end;
	}
	/////////////////////////////////////////////////
	//////
	void Resize(size_t newsize, const T& data)
	{
		size_t oldsize = Size();
		if (newsize <= oldsize)
			_end = _start + newsize;
		else
		{
			size_t capacity = Capacity();
			if(newsize <= capacity)
			{
				for (int i = oldsize; i < newsize; i++)
				{
					_end[i] = data;
				}
				_end = _start + newsize;
			}
			else
			{
				//申请空间
				T* tmp = new T[newsize];
				//拷贝旧元素
				for (int i = 0; i < oldsize; i++)
				{
					tmp[i] = _start[i];
				}
				for (int i = oldsize; i < newsize; i++)
				{
					tmp[i] = data;
				}
				//销毁旧空间
				delete[] _start;
				_start = dtmp;
				_end = _start + newsize;
				_endOfStorage = _end;
			}
		}
	}
	
	void Reserve(size_t newCapacity)
	{
		int oldCapacity = Capacity();
		
		if (newCapacity > oldCapacity)
		{	
			//创建新的空间
			T* tmp = new T[newCapacity];
			size_t size = Size();
			//拷贝元素
			if (_start)
			{
				
				for (int i = 0; i < size; i++)
				{
					tmp[i] = _start[i];
				}
				delete[] _start;
			  }
			_start = tmp;
			_end = _start + size;
			_endOfStorage = _start + newCapacity;
		   }
		
	}

	///////////////////////////////////////////////////////////
	/////Acess
	T& operator[]( size_t index)
	{
		return _start[index];
	}
	const T& operator[](size_t size)const
	{
		return _start[index];
	}
	T& Front()
	{
		return *_start;
	}
	const T& Front()const
	{
		return *_start;
	}
	T& Back()
	{
		return *(_end-1);
	}
	const T& Back()const
	{
		return *(_end-1);
	}
	////////////////////////////////////////////
	//////modify
	void PushBack(const T& data)
	{
		_CheckCapacity();
		*_end++ = data;
	}
	void PopBack()
	{
		if(_start == _end)
			return;
		--_end;
	}
	
	void Insert(size_t pos, const T& data)
	{
		if (_end == _endOfStorage)
			_CheckCapacity();
		size_t size = Size();
		//如果pos等于3,那么对应的数组下标为2 ,就是pos-1
		for (int i = size-1; i >=pos-1; i--)
		{
			_start[i + 1] = _start[i];
		}
		_start[pos - 1] = data;
		_end++;
	}

	void Erase(size_t pos)
	{
		if (_start == _end)
			return;
		size_t size = Size();
		for (int i = pos; i < size; i++)
		{
			_start[i - 1] = _start[i];
		}
		_end--;

	}
	void Clean()
	{
		_end=_start;
	}

	void Swap(Vector<T>& v)
	{
		swap(_start, v._start);
		swap(_end, v._end);
		swap(_endOfStorage, v._endOfStorage);
	}


	void _CheckCapacity()
	{
		if (_end == _endOfStorage)
		{
			int newCapacity = Size() * 2 + 3;
			size_t size = Size();
			T* tmp = new T[newCapacity];
			if (_start)
			{
				//memcpy	浅拷贝
				
				memcpy(tmp, _start, sizeof(T)*size);
				
				//==  深拷贝 
				for (int i = 0; i < size; i++)
				{
					tmp[i] = _start[i];
				}
				delete[]_start;
			}
			_start = tmp;
			_end = _start + size;
			_endOfStorage = _start + newCapacity;
		}

	}




private:
	T* _start;
	T* _end;
	T* _endOfStorage;



};



#include<string>
void TestVector()
{

	int arr[] = { 1, 2, 3, 4, 5, 6 };
	
	Vector <int> str2(arr, arr+sizeof(arr)/sizeof(arr[0]));

	//str2.Resize(10, 3);
	//str2.Reserve(10);
	int ret = str2.Back();
	int* pret = str2.Begin();
	pret = str2.End();
	ret = str2.Front();
	str2.Erase(1);
	str2.Insert(2, 0);
	str2.PopBack();
	str2.PushBack(9);
	Vector<int> str1(1, 2);
	str2.Swap(str1);
	ret = str2[1];


}

int main()

{
	TestVector();
	return 0;
}