vector 模拟与用法

vector 用法

vector

vector 模拟

#pragma once
#include 

namespace sjy
{
	template <typename T>
	class vector
	{
	public:
		//迭代器相关
		typedef T* iterator;
		typedef T* const_iterator;
		iterator begin()
		{
			return _start;
		}
		iterator end()
		{
			return _finish;
		}
		const_iterator begin() const
		{
			return _start;
		}
		const_iterator end() const
		{
			return _finish;
		}

		//默认成员函数相关
		vector()
			:_start(nullptr)
			,_finish(nullptr)
			,_endofstorage(nullptr)
		{}
		vector(size_t n, const T& x = T())
			:_start(nullptr)
			, _finish(nullptr)
			, _endofstorage(nullptr)
		{
			resize(n, x);
		}
		vector(int n, const T& x = T())
			:_start(nullptr)
			, _finish(nullptr)
			, _endofstorage(nullptr)
		{
			resize(n, x);
		}
		template <typename InputIterator>
		vector(InputIterator first, InputIterator last)
		{
			while (first != last)
			{
				push_back(*first);
				first++;
			}
		}
		~vector()
		{
			delete[] _start;
		}
		vector(const vector<T>& other)
		{
			_start = new T[other.capacity()];
			_finish = _start + other.size();
			_endofstorage = _start + other.capacity();
			for (int i = 0; i < other.size(); i++)
			{
				*(_start + i) = *(other._start + i);
			}
		}
		vector<T>& operator=(vector<T> other)
		{
			swap(other);
			return *this;
		}

		//size, capacty相关
		size_t size() const
		{
			return _finish - _start;
		}
		size_t capacity() const
		{
			return _endofstorage - _start;
		}
		void resize(size_t n, const T& x = T())
		{
			if (n < size())
			{
				_finish = _start + n;
			}
			else
			{
				reserve(n);
				while (_finish != _start + n)
				{
					*_finish = x;
					_finish++;
				}
			}
		}
		void reserve(size_t n)
		{
			if (n > capacity())
			{
				size_t sz = size();
				T* tmp = new T[n];
				if (_start != nullptr)
				{
					for (int i = 0; i < sz; i++)
					{
						*(tmp + i) = *(_start + i);
					}
					delete[] _start;
				}
				_start = tmp;
				_finish = _start + sz;
				_endofstorage = _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 == _endofstorage)
			{
				reserve(capacity() == 0 ? 4 : 2 * capacity());
			}
			*_finish = x;
			_finish++;
		}
		iterator insert(iterator pos, const T& x)
		{
			assert(pos >= _start && pos <= _finish);
			if (_finish == _endofstorage)
			{
				size_t len = pos - _start;
				reserve(capacity() == 0 ? 4 : 2 * capacity());
				pos = _start + len;
			}
			for (auto it = _finish; it > pos; it--)
			{
				*it = *(it - 1);
			}
			*pos = x;
			_finish++;
			return pos;
		}

		//删除相关
		iterator erase(iterator pos)
		{
			assert(pos >= _start && pos < _finish);
			for (auto it = pos; it < _finish - 1; it++)
			{
				*it = *(it + 1);
			}
			_finish--;
			return pos;
		}
		void pop_back()
		{
			erase(end() - 1);
		}

		//其他
		void swap(vector<T>& v)
		{
			std::swap(_start, v._start);
			std::swap(_finish, v._finish);
			std::swap(_endofstorage, v._endofstorage);
		}
	private:
		iterator _start;
		iterator _finish;
		iterator _endofstorage;
	};
}

你可能感兴趣的:(C+,+,c++)