#include <iostream>

template <typename iterator>
class reserveIterator {
private:
	iterator current;

public:
	explicit reserveIterator(iterator it) : current(it) {}

	auto& operator*() const {
		return *(current - 1);
	}

	reserveIterator& operator++() {
		--current;
		return *this;
	}

	reserveIterator operator++(int) {
		reserveIterator tmp = *this;
		--current;
		return tmp;
	}

	reserveIterator& operator--() {
		++current;
		return *this;
	}

	reserveIterator operator--(int) {
		reserveIterator tmp = *this;
		++current;
		return tmp;
	}

	reserveIterator operator+(size_t n) {
		return reserveIterator(current - n);
	}

	reserveIterator& operator+=(size_t n) {
		current -= n;
		return *this;
	}

	reserveIterator operator-(size_t n) {
		return reserveIterator(current + n);
	}

	reserveIterator& operator-=(size_t n) {
		current += n;
		return *this;
	}

	bool operator==(const reserveIterator& other) const {
		return current == other.current;
	}

	bool operator!=(const reserveIterator& other) const {
		return current != other.current;
	}
};


template <typename T>
class vector
{
public:
	using iterator = T*;
	using const_iterator = const T*;
	using reserve_iterator = reserveIterator<iterator>;
	using const_reserve_iterator = reserveIterator<const_iterator>;

	vector() :start(nullptr), finish(nullptr), end_of_storage(nullptr) {}
	vector(size_t n, const T& value = T());
	vector(iterator begin, iterator end);
	vector(const vector<T>& other);
	vector(vector<T>&& other);
	vector(std::initializer_list<T> il);
	~vector();
	vector<T>& operator=(const vector<T>& right);
	vector<T>& operator=(vector<T>&& right);
	vector<T>& operator=(std::initializer_list<T> il);

	void reserve(size_t n);
	void resize(size_t n, T& value = T());
	void assign(iterator first, iterator last);
	void assign(size_t n, const T& value);
	void assign(std::initializer_list<T> il);

	size_t size() const { return finish - start; }
	size_t capacity() const { return end_of_storage - start; }
	size_t max_size() const noexcept { return std::numeric_limits<size_t>::max() / sizeof(T); }

	template <typename... Args>
	void emplace_back(Args&&... args);
	template <typename... Args>
	void emplace(iterator position, Args&&... args);

	void push_back(const T& value);
	void push_back(T&& value);
	void pop_back();
	iterator insert(const_iterator position, const T& value);
	iterator insert(const_iterator position, T&& value);
	void insert(const_iterator position, size_t n, const T& value);
	void insert(const_iterator position, const_iterator first, const_iterator last);
	iterator erase(iterator position);
	iterator erase(iterator first, iterator last);
	void clear();
	void swap(vector<T>& right);
	void shrink_to_fit();
	T& at(size_t n);
	const T& at(size_t n) const;
	T& front();
	const T& front() const;
	T& back();
	const T& back() const;

	bool empty() const { return size() == 0; }

	iterator begin()
	{
		return start;
	}

	iterator end()
	{
		return finish;
	}

	const_iterator begin() const
	{
		return start;
	}

	const_iterator end() const
	{
		return finish;
	}

	const_iterator cbegin() const
	{
		return start;
	}

	const_iterator cend() const
	{
		return finish;
	}

	reserve_iterator rbegin()
	{
		return reserve_iterator(end());
	}

	reserve_iterator rend()
	{
		return reserve_iterator(begin());
	}

	const_reserve_iterator rbegin() const
	{
		return const_reserve_iterator(end());
	}

	const_reserve_iterator rend() const
	{
		return const_reserve_iterator(begin());
	}

	const_reserve_iterator rcbegin() const
	{
		return rbegin();
	}

	const_reserve_iterator rcend() const
	{
		return rend();
	}

	T& operator[](size_t pos)
	{
		return *(start + pos);
	}

	const T& operator[](size_t pos) const
	{
		return *(start + pos);
	}

	bool operator==(const vector<T>& lhs, const vector<T>& rhs)
	{
		return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin());
	}

	bool operator!=(const vector<T>& lhs, const vector<T>& rhs)
	{
		return !(lhs == rhs);
	}

	bool operator<(const vector<T>& lhs, const vector<T>& rhs)
	{
		return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
	}

	bool operator<=(const vector<T>& lhs, const vector<T>& rhs)
	{
		return (lhs < rhs) || (lhs == rhs);
	}

	bool operator>(const vector<T>& lhs, const vector<T>& rhs)
	{
		return rhs < lhs;
	}

	bool operator>=(const vector<T>& lhs, const vector<T>& rhs)
	{
		return !(lhs < rhs);
	}

private:
	T* start;
	T* finish;
	T* end_of_storage;

	void destroy(T* first, T* last);
};

template <typename T>
vector<T>::vector(size_t n, const T& value)
{
	reserve(n);
	for (size_t i = 0; i < n; ++i)
	{
		emplace_back(value);
	}
}

template <typename T>
vector<T>::vector(iterator begin, iterator end)
{
	while (begin != end)
	{
		emplace_back(*begin);
		++begin;
	}
}

template <typename T>
vector<T>::vector(const vector<T>& other)
{
	reserve(other.capacity());
	const_iterator other_begin = other.begin();
	while (other_begin != other.end())
	{
		emplace_back(*other_begin);
		++other_begin;
	}
}

template <typename T>
vector<T>::vector(vector<T>&& other) :start(other.start), finish(other.finish), end_of_storage(other.end_of_storage)
{
	other.start = other.finish = other.end_of_storage = nullptr;
}


template <typename T>
vector<T>::vector(std::initializer_list<T> il)
{
	insert(start, il.begin(), il.end());
}

template <typename T>
vector<T>::~vector()
{
	delete[] start;
	start = finish = end_of_storage = nullptr;
}

template <typename T>
vector<T>& vector<T>::operator=(const vector<T>& right)
{
	if (this != &right)
	{
		destroy(start, finish);
		finish = start;
		insert(start, right.begin(), right.end());
	}
	return *this;
}

template <typename T>
vector<T>& vector<T>::operator=(vector<T>&& right)
{
	if (this != &right)
	{
		destroy(start, finish);
		start = right.start;
		finish = right.finish;
		end_of_storage = right.end_of_storage;
		right.start = right.finish = right.end_of_storage = nullptr;
	}
	return *this;
}

template <typename T>
vector<T>& vector<T>::operator=(std::initializer_list<T> il)
{
	assign(il);
	return *this;
}

template <typename T>
void vector<T>::reserve(size_t n)
{
	if (n > capacity())
	{
		T* temp = new T[n];
		size_t vsize = size();
		if (start) {
			for (size_t i = 0; i < vsize; ++i) {
				temp[i] = start[i];
			}
			delete[] start;
		}
		start = temp;
		finish = temp + vsize;
		end_of_storage = temp + n;
	}
}

template <typename T>
void vector<T>::resize(size_t n, T& value)
{
	if (n < size())
	{
		finish = start + n;
	}
	else
	{
		if (n > capacity)
		{
			reserve(n);
		}
		while (finish != start + n)
		{
			emplace_back(value);
		}
	}
}

template <typename T>
void vector<T>::assign(iterator first, iterator last)
{
	clear();
	insert(start, first, last);
}

template <typename T>
void vector<T>::assign(size_t n, const T& value)
{
	clear();
	reserve(n);
	for (size_t i = 0; i < n; ++i)
	{
		emplace_back(value);
	}
}

template <typename T>
void vector<T>::assign(std::initializer_list<T> il)
{
	clear();
	insert(start, il.begin(), il.end());
}

template <typename T>
template <typename... Args>
void vector<T>::emplace_back(Args&&... args)
{
	if (finish == end_of_storage)
	{
		reserve(capacity() == 0 ? 1 : capacity() * 2);
	}
	new (finish) T(std::forward<Args>(args)...);
	++finish;
}

template <typename T>
template <typename... Args>
void vector<T>::emplace(iterator position, Args&&... args)
{
	if (position < start || position > finish)
	{
		throw std::out_of_range("out of range");
	}
	if (finish == end_of_storage)
	{
		size_t vsize = position - start;
		reserve(capacity() == 0 ? 1 : capacity() * 2);
		position = start + vsize;
	}
	for (iterator it = finish; it != position; --it)
	{
		*it = std::move(*(it - 1));
	}
	new (position) T(std::forward<Args>(args)...);
	++finish;
	return position;
}

template <typename T>
void vector<T>::push_back(const T& value)
{
	emplace_back(value);
}

template <typename T>
void vector<T>::push_back(T&& value)
{
	emplace_back(std::move(value));
}

template <typename T>
void vector<T>::pop_back()
{
	if (empty)
	{
		throw std::out_of_range("vector is empty");
	}
	destroy(finish - 1, finish);
	--finish;
}

template <typename T>
typename vector<T>::iterator vector<T>::insert(const_iterator position, const T& value)
{
	emplace(position, value);
}

template <typename T>
typename vector<T>::iterator vector<T>::insert(const_iterator position, T&& value)
{
	emplace(position, std::move(value));
}

template <typename T>
void vector<T>::insert(const_iterator position, size_t n, const T& value)
{
	if (position < start || position > finish)
	{
		throw std::out_of_range("out of range");
	}
	if (finish + n > end_of_storage)
	{
		size_t vsize = position - start;
		reserve(size() + n);
		position = start + vsize;
	}
	std::move_backward(position, finish, finish + n);
	std::fill(position, position + n, value);
	finish += n;
}

template <typename T>
void vector<T>::insert(const_iterator position, const_iterator first, const_iterator last)
{
	if (position < start || position > finish)
	{
		throw std::out_of_range("out of range");
	}
	size_t p_size = position - start;
	size_t old_size = finish - start;
	for (; first != last; ++first)
	{
		emplace_back(*first);
	}
	std::rotate(start + p_size, start + old_size, finish);

}

template <typename T>
typename vector<T>::iterator vector<T>::erase(iterator position)
{
	if (position < start || position >= finish)
	{
		throw std::out_of_range("out of range");
	}
	for (iterator it = position; it != finish - 1; ++it)
	{
		*it = std::move(*(it + 1));
	}
	destroy(finish - 1, finish);
	--finish;
	return position;
}

template <typename T>
typename vector<T>::iterator vector<T>::erase(iterator first, iterator last)
{
	if (first >= last || first < start || last > finish)
	{
		throw std::out_of_range("out of range");
	}
	iterator new_finish = std::move(last, finish, first);
	destroy(new_finish, finish);
	finish = new_finish;
	return first;
}


template <typename T>
void vector<T>::clear()
{
	destroy(start, finish);
	finish = start;
}


template <typename T>
void vector<T>::swap(vector<T>& right)
{
	std::swap(start, right.start);
	std::swap(finish, right.finish);
	std::swap(end_of_storage, right.end_of_storage);
}

template <typename T>
void vector<T>::shrink_to_fit()
{
	if (end_of_storage != finish)
	{
		end_of_storage = finish;
	}
}

template <typename T>
T& vector<T>::at(size_t n)
{
	if (n >= size())
	{
		throw std::out_of_range("out of range");
	}
	return *(start + n);
}

template <typename T>
const T& vector<T>::at(size_t n) const
{
	if (n >= size())
	{
		throw std::out_of_range("out of range");
	}
	return *(start + n);
}

template <typename T>
T& vector<T>::front()
{
	if (empty())
	{
		throw std::out_of_range("vector is empty");
	}
	return *start;
}

template <typename T>
const T& vector<T>::front() const
{
	if (empty())
	{
		throw std::out_of_range("vector is empty");
	}
	return *start;
}

template <typename T>
T& vector<T>::back()
{
	if (empty())
	{
		throw std::out_of_range("vector is empty");
	}
	return *(finish - 1);
}

template <typename T>
const T& vector<T>::back() const
{
	if (empty())
	{
		throw std::out_of_range("vector is empty");
	}
	return *(finish - 1);
}

template <typename T>
void vector<T>::destroy(T* first, T* last)
{
	while (first != last)
	{
		first->~T();
		++first;
	}
}

帮我看看代码中的迭代器可以封装起来吗