模拟实现Vector

Vector模拟实现,具体的Vector描述请查看https://blog.csdn.net/weixin_40853073/article/details/81413281

#include<iostream>
#include<stdio.h>
#include<assert.h>
using namespace std;
#pragma once 
typedef int DataType;
class Vector
{
public:
	Vector()//使用初始化列表初始化
		:_first(NULL)//相当于线性表的数组名
		, _finish(NULL)//相当于线性表的元素个数
		, _endofstorage(NULL)//相当于线性表的容量
	{}
	Vector(const Vector& v)//拷贝构造
	{
		
		_first = new DataType[v.Size()];//开辟一个新的_first,为他开辟大小为_finish的空间
		_finish = _first + v.Size();
		_endofstorage = _first + v.Size();
		memcpy(_first, v._first, sizeof(DataType)*v.Size());

	}
	//v1=v2
	Vector& operator=(Vector& v)//赋值重载的现代写法
	{
		swap(_first, v._first);
		swap(_finish, v._finish);
		swap(_endofstorage, v._endofstorage);
		return *this;//v是局部对象,出了作用域释放

	}
	DataType& operator[](size_t pos)
	{
		return _first[pos];
	}
	~Vector()//析构函数
	{
		if (_first)
		{
			delete[] _first;//开辟的数组需要释放
			_first = _finish = _endofstorage = NULL;
		}
	}
	size_t Size() const
	{
		return _finish - _first;//元素个数
	}
	size_t Capacity() const
	{
		return _endofstorage - _first;//容量
	}
	void PushBack(DataType x)//尾插
	{
		if (_finish == _endofstorage)//首先判断是否需要扩容
		{
			size_t newcapacity = Capacity() > 0 ? Capacity() * 2 : 3;//最初容量为0时,直接把容量赋值为3;当容量不为0时增容*2
			Expand(newcapacity);
		}

		*_finish = x;
		++_finish;
	}
	void PopBack()//尾删
	{
		assert(_finish > _first);
		--_finish;
	}
	
	void Insert(size_t pos, DataType x)//在指定位置插入数字
	{
		if (pos > Size())
		{
			printf("错误的插入地点");
		}
		else
		{
			DataType end = Size()-1;//end为数组的最后一个位置
			if (_finish == _endofstorage)//判断是否扩容
			{
				Expand(end*2);
			}
			while (end > pos-1)//循环后移
			{
				_first[end + 1] = _first[end];
				end--;
			}
			_first[pos] = x;
			_finish++;
		}
	}
	void Erase(size_t pos)//删除指定位置的数字
	{
		if (pos > Size())
		{
			printf("错误的位置");
		}
		else
		{
			size_t end = Size() - 1;
			while (pos < end)
			{
				_first[pos] = _first[pos + 1];//循环前移
				pos++;
			}
			_finish--;
		}
	}
      size_t Find(DataType x)//查找数字是否存在
	  {
		size_t tmp = Size() - 1;
		size_t i = 0;
		for (i = 0; i <= tmp; i++)
		{
			if (_first[i] == x)
			{
				printf("找到了,这个数是 %d \n", _first[i]);
				return 0;
			}
		}
		printf("没找到\n");
		return -1;
	}
private:
	void Expand(size_t n)//扩容
	{
		if (n > Capacity())
		{
			size_t size = Size();
			size_t capacity = Capacity();
			DataType*tmp = new DataType[n];
			memcpy(tmp, _first, Size()*sizeof(DataType));
			delete[] _first;
			_first = tmp;
			_finish = _first + size;
			_endofstorage = _first + n;
		}
	}
	DataType* _first;
	DataType* _finish;//最后一个的下一个
	DataType* _endofstorage;//capcity的下一个
};
int main()
{
	Vector v1;
	v1.PushBack(1);
	v1.PushBack(2);
	v1.PushBack(3);
	v1.PushBack(4);	
	v1.PushBack(5);
	v1.PushBack(6);
	v1.Insert(3, 7);
	v1.Insert(3, 7);
	v1.Insert(3, 7);
	v1.Insert(3, 7);
	v1.Insert(3, 7);
	v1.Insert(3, 7);
	v1.Insert(3, 2);
	v1.Insert(3, 22);
	v1.Erase(3);
	v1.Find(77);
	v1.PopBack();
	//Vector v2;
	//v2 = v1;//赋值重载
	//Vector v2(v1);//拷贝构造
	for (size_t i = 0; i < v1.Size(); i++)//打印
	{
		cout << v1[i] << " ";
	}
	cout << endl;
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_40853073/article/details/83052280