模拟实现vector容器

//模拟实现vector

#include <iostream>
#include <assert.h>
#include <algorithm>
#include <string>
using namespace std;

namespace mylib
{
	template<class T>
	class Vector
	{
	public:
		//vector的迭代器就是一个原生指针
		typedef T* Iterator;
		typedef const T* ConstIterator;

		Iterator Begin() { return _start; };
		Iterator End() { return _finish; }

		ConstIterator CBegin() const { return _start; }
		ConstIterator CEnd() const { return _finish; }

		size_t Size() const
		{
			return _finish - _start;
		}

		size_t Capacity() const
		{
			return _endOfStorage - _start;
		}

		Vector()
			:_start(nullptr)
			, _finish(nullptr)
			,_endOfStorage(nullptr)
		{}

		//对应vector(4,100)
		Vector(int n, const T& value = T())
			:_start(nullptr)
			, _finish(nullptr)
			, _endOfStorage(nullptr)
		{
			Reserve(n);   //Reserve 需要模拟实现
			while (n--)
			{
				PushBack(value);  //PushBack需要模拟实现
			}
		}

		//使用两个迭代器进行初始化构造
		template<class InputIterator>
		Vector(InputIterator first, InputIterator last)
		{
			Reserve(last - first);
			while (first != last)
			{
				PushBack(*first);
				first++;
			}
		}

		//const拷贝构造
		Vector(const Vector<T>& v)
			:_start(nullptr)
			, _finish(nullptr)
			, _endOfStorage(nullptr)
		{
			Reserve(v.Capacity());

			Iterator it = Begin();
			ConstIterator vit = v.CBegin();
			while (vit != v.CEnd())
			{
				*it++ = *vit++;
			}

			_finish = _start + v.Size();
			_endOfStorage = _start + v.Capacity();
		}

		Vector<T>& operator=(Vector<T> v)
		{
			Swap(v);  //需要自己定义
			return *this;
		}

		~Vector()
		{
			delete[] _start;
			_start = _finish = _endOfStorage = nullptr;
		}

		//对原来的空间大小进行修改,Reserve 不会对扩充的内容进行初始化
		void Reserve(size_t n)
		{
			if (n > Capacity())
			{
				//需要扩容
				size_t size = Size();
				T* tmp = new T[n];

				if (_start)
				{
					for (int i = 0; i < size; ++i)
					{
						tmp[i] = _start[i];
					}
				}
				_start = tmp;
				_finish = _start + size;
				_endOfStorage = _start + n;
			}
		}

		//n 表示个数,要扩充多少个
		void Resize(int n, const T& value = T())
		{
			//1.如果n小于当前size,不用扩充,要将数据缩小到n
			if (n <= Size())
			{
				_finish = _start + n;
				_endOfStorage = _start + n;
				return;
			}

			//2.空间不够,需要扩容
			if (n > Capacity())
			{
				Reserve(n);
			}

			//3.将size扩大到n
			Iterator it = _finish;
			Iterator _finish = _start + n;
			while (it != _finish)
			{
				*it = value;
				++it;
			}
		}

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

		void PushBack(const T& x)
		{
			Insert(End(), x);   //需要自己实现
		}

		void PopBack()
		{
			Erase(--End());  //需要实现
		}

		Iterator Insert(Iterator pos, const T& x)
		{
			assert(pos <= _finish);

			//空间不够,先进行增容
			if (_finish == _endOfStorage)
			{
				int size = Size();
				//int newCapacity = Capacity() * 2;
				int newCapacity = (Capacity() == 0) ? 1 : Capacity() * 2;
				Reserve(newCapacity);

				//如果发生了增容,需要重置pos
				pos = _start + size;
			}
			//搬移数据
			Iterator end = _finish - 1;
			while (end >= pos)
			{
				*(end + 1) = *end;
				--end;
			}

			*pos = x;
			++_finish;
			return pos;
			
		}

		//返回删除数据的下一个数据,方便解决一边遍历一边删除的迭代器失效问题
		Iterator Erase(Iterator pos)
		{
			Iterator begin = pos + 1;
			while (begin != _finish)
			{
				*(begin - 1) = *begin;
				++begin;
			}
			--_finish;
			return pos;
		}

		T& operator[](int pos)
		{
			return _start[pos];
		}



	 private:
		Iterator _start;     //指向数据块的开始
		Iterator _finish;    //指向数据块的尾
		Iterator _endOfStorage;     //指向存储容量的尾
	};

}



//对mylib::vector进行测试
//vector的构造部分测试
void TestVector1()
{
	mylib::Vector<int> first;   //整型类型的空vector

	mylib::Vector<int> second(4,100); //

	mylib::Vector<int> third(second.Begin(), second.End());

	mylib::Vector<int> fourth(third);
	
	int myints[] = { 15,2,7,3 };
	mylib::Vector<int> fifth(myints, myints + sizeof(myints) / sizeof(myints[0]));
	cout << "fifth contents:"<<endl;
	for (mylib::Vector<int>::Iterator it = fifth.Begin(); it != fifth.End(); ++it)
	{
		cout << *it << " ";
	}
	cout << endl;

	mylib::Vector<string> strV;
	strV.PushBack("1111");
	strV.PushBack("2222");
	strV.PushBack("3333");
	strV.PushBack("4444");
	strV.PushBack("5555");

	for (int i = 0; i < strV.Size(); ++i)
	{
		cout << strV[i] << " ";
	}
	cout << endl;

}


void PrintVector(const mylib::Vector<int>& v)
{
	mylib::Vector<int>::ConstIterator cit = v.CBegin();
	while (cit != v.CEnd())
	{
		cout << *cit << " ";
		cit++;
	}
	cout << endl;
	return;
}

void TestVector2()
{
	mylib::Vector<int> v;
	v.PushBack(1);
	v.PushBack(2);
	v.PushBack(3);
	v.PushBack(4);

	//使用迭代器进行遍历打印
	mylib::Vector<int>::Iterator it = v.Begin();
	while (it != v.End())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;

	//使用迭代器进行修改
	it = v.Begin();
	while (it != v.End())
	{
		*it *= 2;
		it++;
	}
	PrintVector(v);
}

//测试reserve/resize
void TestVector3()
{
	//扩容问题
	/*mylib::Vector<int> v1;
	int size = v1.Capacity();
	cout << "扩充前:" << size << endl;
	for (int i = 0; i < 100; i++)
	{
		v1.PushBack(i);
		if (v1.Capacity() != size)
		{
			size = v1.Capacity();
			cout << "扩容后:" << size << endl;

		}
	}*/

	//reserve
	//mylib::Vector<int> v2;
	//v2.Reserve(100);
	//int size = v2.Capacity();
	//cout << "扩充前:" << size << endl;
	//for (int i = 0; i < 100; i++)
	//{
	//	v2.PushBack(i);
	//	if (v2.Capacity() != size)
	//	{
	//		size = v2.Capacity();
	//		cout << "扩容后:" << size << endl;

	//	}
	//}
	
	//resize
	mylib::Vector<int> v3;


	for (int i = 0; i < 4; i++)
	{
		v3.PushBack(i);
	}
	PrintVector(v3);

	v3.Resize(2, 2);
	PrintVector(v3);

}


#include <vector>
//int main()
//{
//	//vector<vector<int>> vec(6,  vector<int>(5,7));
//	//TestVector1();
//	//TestVector2();
//	TestVector3();
//
//	vector<int> aaa(10); //10个数,默认每个数都是0
//	return 0;
//}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值