简单实现STL vector

简单实现STL vector,第1张

简单实现STL vector
#include 
#include 
#include 
#include 


namespace my
{

	template
	class vector
	{

	public:
		typedef T* iterator;
		typedef const T* const_iterator;
	private:
		iterator _start;
		iterator _finish;
		iterator _endofstorage;

	public:

		vector()
			:_start(nullptr),
			_finish(nullptr),
			_endofstorage(nullptr)
		{

		}

		vector(const vector& x)
			:_start(nullptr),
			_finish(nullptr),
			_endofstorage(nullptr)
		{
			//传统写法
			//size_t sz = x.size();
			//T* tmp = new T[x.capacity()];
			//memmove(tmp, x._start, sz * sizeof(T));
			//_start = tmp;
			//_finish = _start + sz;
			//_endofstorage = _start + x.capacity();

			//简化写法
			vector tmp(x._start, x._finish);
			swap(tmp);

		}


		template
		vector(InputIterator first, InputIterator last)
			:_start(nullptr),
			_finish(nullptr),
			_endofstorage(nullptr)
		{
			while (first != last)
			{
				push_back(*first);
				first++;
			}
		}


		~vector()
		{
			delete[] _start;
		}

		size_t size() const
		{
			return _finish - _start;
		}
		size_t capacity() const
		{
			return _endofstorage - _start;
		}
		bool empty()
		{
			return _start == _finish;
		}

		iterator begin()
		{
			return _start;
		}
		iterator end()
		{
			return _finish;
		}
		void reserve(size_t n)
		{
			T* tmp = new T[n];

			int len = size();
			for (int i = 0; i < len; i++)
			{
				tmp[i] = _start[i];
			}

			//使用memmove,在存储string这种对象时,会引起浅拷贝问题
			//memmove(tmp, _start, sizeof(T) * len);
			delete[] _start;

			_start = tmp;
			_finish = _start + len;
			_endofstorage = _start + n;
		}
		void resize(size_t n, const T& x = T())
		{
			if (n <= size())
			{
				_finish = _start + n;
			}
			else if (n <= capacity())
			{

				while (_finish < _endofstorage)
				{
					*_finish = x;
					_finish++;
				}

			}
			else
			{
				int sz = size();
				T* tmp = new T[n];
				memmove(tmp, _start, sz * sizeof(T));
				_start = tmp;
				_finish = _start + sz;
				_endofstorage = _start + n;
				while (_finish < _endofstorage)
				{
					*_finish = x;
					_finish++;
				}
			}
		}
		void swap(vector& v)
		{
			std::swap(_start, v._start);
			std::swap(_endofstorage, v._endofstorage);
			std::swap(_finish, v._finish);
		}


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

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

			return  _start[pos];
		}
		vector& operator=(const vector& v)
		{
			if (this != &v)
			{
				vector tmp(v);
				swap(tmp);
			}

			return *this;
		}
	


		void push_back(const T& x)
		{
			if (_finish == _endofstorage)
			{
				//扩容
				size_t newCapacity = capacity() == 0 ? 4 : capacity() * 2;
				reserve(newCapacity);
			}

			*_finish = x;
			++_finish;
		}
		void pop_back()
		{
			assert(!empty());
			_finish--;
		}
		iterator insert(iterator pos, const T& x)
		{
			assert(pos >= begin() && pos <= _finish);
			if (_finish == _endofstorage)
			{
				int len = pos - _start;
				size_t newCapacity = capacity() == 0 ? 4 : capacity() * 2;
				reserve(newCapacity);
				pos = _start + len;
			}

			iterator end = _finish - 1;
			while (end >= pos)
			{
				*(end + 1) = *end;
				end--;
			}

			end++;
			*end = x;
			_finish++;

			return end;
		}
		iterator erase(iterator pos)
		{
			assert(pos >= _start && pos < _finish);

			iterator cur = pos;
			while (cur < _finish - 1)
			{
				*cur = *(cur + 1);
				cur++;
			}

			_finish--;
			return pos;
		}

		void print()
		{
			for (auto i : *this)
			{
				cout << i << ' ';
			}
			cout << endl;
		}

	};
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5502426.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-13
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存