c++ :vector的简单实现

vector:    

vector(向量): C++中的一种数据结构,确切的说是一个类.它相当于一个动态的数组,当程序员无法知道自己需要的数组的规模多大时,用其来解决问题可以达到最大节约空间的目的.可以把它理解为一个顺序表或者数组。只是STL里的vector是由三个迭代器来维护的:_str(数组存放开始的位置),finsh(数据存放结束位置的下一个),_endofstorage(容量的最后一个位置)。vector里的迭代器其实就是指针。


#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;
typedef int DataType;

class Vector
{
public:
	Vector()
		:_start(NULL)
		, _finish(NULL)
		, _endofstorage(NULL)
	{}
	Vector(const Vector& v)
	{
		if (this != &v)
		{
			DataType* pcur = v._start;
			while (pcur < v._finish)
			{
				PushBack(*pcur);
				pcur++;
			}
		}
	}
	//v1 = v2
	Vector& operator = (Vector v)
	{
		Swap(v);
		return *this;
	}
	//v1.Swap(v2)
	void Swap(Vector& v)
	{
		swap(_start, v._start);
		swap(_finish, v._finish);
		swap(_endofstorage, v._endofstorage);
	}
	~Vector()
	{
		if (_start != NULL)
		{
			delete[] _start;
			_start = _finish = _endofstorage = NULL;
		}
	}
	size_t Size()
	{
		return _finish - _start;
	}
	size_t Capacity()
	{
		return _endofstorage - _start;
	}
	void Expand(size_t n)
	{
		if (n <= Capacity())
		{
			return;
		}
		//建立一个新空间
		DataType* newstart = new DataType[n];
		DataType* newfinish = newstart;
		DataType* newend = newstart + n;//一般扩大两倍
										//newstart和newfinish在同一位置,将newfinish移动到合适位置
		DataType* pcur = _start;
		while (pcur < _finish)
		{
			//赋值
			*newfinish = *pcur;
			//两个空间同时走
			pcur++;
			newfinish++;
		}
		//释放原空间
		delete[] _start;
		_start = newstart;
		_finish = newfinish;
		_endofstorage = newend;
	}

	void PushBack(DataType x)
	{
		if (_finish == _endofstorage)
		{
			size_t newcapacity = (_endofstorage == NULL ? 3 : Capacity() * 2);
			Expand(newcapacity);
		}
		*_finish++ = x;
	}
	void Reserve(size_t n)
	{
		Expand(n);
	}
	void PopBack()
	{
		if (_finish != _start && _finish != NULL)
		{
			--_finish;
		}
		else
		{
			cout << "The Vector is empyt!" << endl;
		}
	}
	void Insert(size_t pos, DataType x)
	{
		if (_start + pos > _endofstorage)
		{
			cout << " The pos illegal " << endl;
			return;
		}
		if (_finish == _endofstorage)
		{
			Expand(Capacity());
		}
		if (_start + pos == _finish)
		{
			PushBack(x);
		}
		else
		{
			DataType* pcur = _finish;
			while (pcur >_start + pos )
			{
				*pcur = *(pcur - 1);
				--pcur;
			}
			*pcur = x;
			_finish++;
		}
	}
	void Erase(size_t pos)
	{

		if (_start + pos > _endofstorage)
		{
			cout << " The pos illegal " << endl;
			return;
		}
		if (_start + pos == _finish - 1)
		{
			PopBack();
		}
		else
		{
			while (*(_start + pos) != *(_finish))
			{
				*(_start + pos) = *(_start + pos + 1);
				pos++;
			}
			_finish--;
		}
	}
	size_t Find(DataType x)
	{
		if (_start != NULL)
		{
			DataType* pcur = _start;
			while (pcur < _finish)
			{
				pcur++;
				if (*pcur == x)
				{
					return pcur - _start;
				}
			}
		}
		return -1;
	}
	void show()
	{
		if (_start == NULL)
		{
			cout << "empty" << endl;
		}
		else
		{
			cout << "<:" ;
			DataType* pcur = _start;
			while (pcur < _finish)
			{
				cout << *pcur<<" ";
				pcur++;
			}
			cout << endl;
		}
	}
private:
	DataType * _start;
	DataType* _finish;
	DataType* _endofstorage;
};

void test()
{
	Vector v;
	v.PushBack(1);
	v.PushBack(2);
	v.PushBack(3);
	v.PushBack(4);
	v.PushBack(5);
	v.PushBack(6);
	v.PushBack(7);
	v.PushBack(8);
	v.show();
	v.PopBack();
	v.show();
	v.Erase(0);
	v.show();
	v.Insert(0,1);
	v.show();
}
int main()
{
	test();
	system("pause");
	return 0;
}


猜你喜欢

转载自blog.csdn.net/W_J_F_/article/details/82669412