STL中vector的底层实现

namespace MyVector{
	template<class T>
	class vector{
		public:
		typedef   T*  iterator;
		typedef const T*  const_iterator;

		iterator begin()
		{
			return _start;
		}

		iterator end()
		{
			return _finish;
		}

		const_iterator cbegin() const
		{
			return _start;
		}

		const_iterator cend() const
		{
			return _finish;
		}



			

		vector()
			:_start(0), _finnish(0), _end(0)
		{ }
		//v(v1)
		vector(int n, const T& value = T())
		{
			_start = new T[n];
			for (int i = 0; i < n; ++i)
			{
				_start[i] = value;
			}
			_finish = _start + n;
			_end = _finish;

		}

			template<class InputIterator>

			vector(InputIterator first, InputIterator last)
				:_start(0), _finish(0), _end(0)
			{
				for (; first != last; ++first)
					push_back(*frist);
			}
			vector(const vector<T>& v)
			{
				_start = new T[v.size()];
				_finish = _start + v.size();
				_end = _finish;
				memcpy(_start, v._start, sizeof(T)*v.size());
			}

			vector<T>& operator= (vector<T> v)
			{
				this->swap(v);
				return *this;
			}

				~vector()
			{
					delete[] _start;
					_start = nullptr;
					_finish = nullptr;
					_end = nullptr;
			}

			

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

				size_t capacity() const
				{
					return _end - _start;
				}

				void reserve(size_t n)
				{
					if (n > capacity())
					{
						size_t sz = size();
						T* tmp = new T[n];
						memcpy(tmp, _start, sizeof(T)*sz);
						delete[] _start;
						_start = tmp;
						_finish = _start + sz;
						_end = _start + n;
					}
				}

				void resize(size_t n, const T& value = T())
				{
					if (n > size())
					{

						sz = size();
						if (n>capacity())
						 reserve(n);
						memset(_start + sz, value, sizeof(T)*(n - sz));
					}
						else
						{

							_finish = _start + n;
						}
					
				}



		

				T& operator[](size_t pos)
				{
					assert(pos < size());
					return _start[pos];
				}

				const T& operator[](size_t pos)const
				{
					assert(pos < size());
					return _start[pos];

				}



			void push_back(const T& x)
			{   //空间满了,需要扩展空间
				/*if (_finish == _end)
				{
					size_t cap = capacity() == 0 ? 2 : capacity() * 2;
					reserve(cap);
				}
				_start[size()] = x;
				++_finish;*/
				insert(end(), x);
			}

			void pop_back()
			{
				/*assert(size() > 0);
				--_finish;*/
				erase(end()-1);
			}

			void swap(vector<T>& v)
			{
				::swap(_start, v._start);
				::swap(_finish, v._finnish);
				::swap(_end, v._end);
			}

			iterator insert(iterator pos, const T& x)
			{
				assert(pos <= _finish);
				if (_finish == _end)
				{
					int len = pos - _start;
					size_t cap = capacity() == 0 ? 2 : capacity() * 2;
					reserve(cap);
					pos = _start + len;
				}
				size_t sz = size() + 1;
				while (_finish != pos)
				{
					*(_finish + 1) = *_finish;
					--_finish;
				}
				*pos = x;
				_finish = _start + sz;
				return pos;
			}

			iterator erase(iterator pos)
			{
				assert(pos<_finish&&pos>=_start);
				iterator it = pos + 1;
				while (it != _finish)
				{
					*(it-1)= *it;
					++it;
				}
				--_finish;
				return pos;
			}
	private:
		iterator  _start;
		iterator  _finish;
		iterator  _end;
	};


};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值