C++vector简单实现

本文介绍了C++中的vector模板类的构造函数、析构函数、容量管理、迭代器以及基本操作方法,如插入、删除和调整大小等,特别提到了迭代器失效的情况需注意。
摘要由CSDN通过智能技术生成

由于我们之前已经详细讲解了string接口,而vector接口大都在string上有,所以大家只需自行翻阅前面文章就可以明白接口的使用了,所以,这里我们只实现vector,注意:vector会有迭代器失效的情况,大家一定要仔细看看代码。

#pragma once
#include <assert.h>
#include <iostream>
#include <string.h>
namespace cx
{
	//模板类
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;
		//构造函数:
		vector()
		{}
		//传统写法
		/*vector(const vector<T>& v)
		{
			_start = new T[v.capacity()];
			memcpy(_start, v._start, v.size() * sizeof(T));
			_finish = _start + v.size();
			_endofstorage = _start + v.capacity();
		}*/
		//现代写法:
		vector(const vector<T>& v)
		{
			reserve(v.capacity());
			for (const auto e : v)
			{
				*_finish = e;
				_finish++;
			}
		}
		vector(size_t n, const T& val = T())
		{
			resize(n,val);
		}
		/*template <class InputIterator>
		vector(InputIterator first, InputIterator last)
		{
			while (first != last)
			{
				push_back(*first);
				first++;
			}
		}*/
		//析构函数
		~vector()
		{
			if (_start)
			{
				delete[] _start;
				_start = _finish = _endofstorage = nullptr;
			}
		}
		vector<T>& operator=(vector<T> v)
		{
			swap(v);
			return *this;
		}
		//迭代器
		iterator begin()
		{
			return _start;
		}
		iterator end()
		{
			return _finish;
		}
		const_iterator begin()const
		{
			return _start;
		}
		const_iterator end()const
		{
			return _finish;
		}
		//计算大小
		size_t size()const 
		{
			return end() - begin();
		}
		size_t capacity()const 
		{
			return _endofstorage - _start;
		}
		//操作:
		bool empty() const
		{
			return _finish == _start;
		}
		void swap(vector<T>& v)
		{
			std::swap(_start, v._start);
			std::swap(_finish, v._finish);
			std::swap(_endofstorage, v._endofstorage);
		}
		void reserve(size_t n)
		{
			if (n > capacity())
			{
				size_t old = size();
				T* tmp = new T[n];
				if (_start)
				{
					memcpy(tmp, _start, sizeof(T) * old);
					delete[] _start;
				}
				_start = tmp;
				_finish = _start + old;
				_endofstorage = _start + n;
			}
		}
		void push_back(const T x)
		{
			//检查是否扩容
			if (_finish == _endofstorage)
			{
				size_t newcapacity = capacity() == 0 ? 4 : 2 * capacity();
				reserve(newcapacity);
			}
			*_finish = x;
			_finish++;
		}
		void pop_back()
		{
			assert(size() > 0);
			_finish--;
		}
		T& operator[](size_t pos)
		{
			assert(pos < size());
			return _start[pos];
		}
		T& operator[] (size_t pos) const
		{
			assert(pos < size());
			return _start[pos];
		}
		iterator insert(iterator pos, const T& val)
		{
			assert(pos <= _finish && pos >= _start);
			if (_finish == _endofstorage)
			{
				size_t len = pos - _start;
				reserve(capacity() == 0 ? 4 : 2 * capacity());
				pos = _start + len;
			}
			//memmove(pos + 1, pos, sizeof(T) * (_finish - pos));
			iterator end = _finish - 1;
			while (end >= pos)
			{
				*(end + 1) = *(end);
				end--;
			}
			*pos = val;
			_finish++;
			return pos;
		}
		/*void insert(iterator pos, const T& x)
		{
			//检查
			assert(pos >= _start);
			assert(pos <= _finish);
			if (_finish == _endofstorage)
			{
				size_t len = pos - _start;
				reserve(capacity() == 0 ? 4 : 2 * capacity());
				//注意:pos位置也要改变
				pos = _start + len;
			}
			memmove(pos + 1, pos, sizeof(T) * (_finish - pos));
			*pos = x;
			_finish++;
		}*/
		iterator erase(iterator pos)
		{
			assert(pos < _finish && pos >= _start);
			iterator it = pos + 1;
			while (it < _finish)
			{
				*(it - 1) = *it;
				it++;
			}
			_finish--;
			return pos;
		}
		iterator erase(iterator first, iterator last)
		{

		}
		void resize(size_t n, T val = T())
		{
			if (n > size())
			{
				reserve(n);
				while (_finish < _start + n)
				{
					*_finish = val;
					_finish++;
				}
			}
			else
			{
				_finish = _start + n;
			}
		}
		T& front()
		{
			return *_start;
		}
		const T& front() const
		{
			*_start;
		}
		T& back()
		{
			return *_finish;
		}
		const T& back() const
		{
			return *_finish;
		}
		T& at(size_t pos)
		{
			assert(pos < size());
			return _start[pos];
		}
		const T& at(size_t pos) const
		{
			assert(pos < size());
			return _start[pos];
		}
		void assign(size_t n, const T& val)
		{
			assert(_start == _finish);
			reserve(n);
			while (_finish != _endofstorage)
			{
				*_finish = val;
				_finish++;
			}
		}
		void clear()
		{
			resize(0);
		}
	private:
		iterator _start = nullptr;
		iterator _finish = nullptr;
		iterator _endofstorage = nullptr;
	};
}

最后,感谢大家的支持!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jiaofi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值