vector类的模拟实现(包括vector的构造,拷贝构造, 赋值运算符重载,中括号运算符重载, 迭代器, 增删改查, 包括三种insert, 两种erase)

vector类的模拟实现

Vector.h

#pragma once
#include 
#include 

using namespace std;

namespace gwp
{


template 
class vector
{
public:
	typedef T* iterator;
	typedef const T* const_iterator;

	iterator begin()
	{
		return m_start;
	}

	iterator end()
	{
		return m_finish;
	}

	const_iterator cbegin()
	{
		return m_start;
	}

	const_iterator cend()
	{
		return m_finish;
	}

	iterator rbegin()
	{
		return m_finish;
	}

	iterator rend()
	{
		return m_start;
	}

	const_iterator rcbegin()
	{
		return m_finish;
	}

	const_iterator rcend()
	{
		return m_start;
	}

	vector()
		:m_start(nullptr)
		, m_finish(nullptr)
		, m_endOfStorage(nullptr)
	{

	}

	vector(int n, const T& value = T())
		:m_start(nullptr)
		, m_finish(nullptr)
		, m_endOfStorage(nullptr)
	{
		reserve(n);
		while (n--)
		{
			push_back(value);
		}
	}

	template 
	vector(InputIterator first, InputIterator last)
	{
		reserve(last - first);

		while (first != last)
		{
			push_back(*first);
			first++;
		}
	}

	vector(const vector& v)
		:m_start(nullptr)
		, m_finish(nullptr)
		, m_endOfStorage(nullptr)
	{
		reserve(v.capacity());
		
		iterator it = begin();
		const_iterator vit = v.cbegin();

		while (vit != v.end())
		{
			*it++ = *vit++;
		}

		m_finish = m_start + v.size();
		m_endOfStorage = m_start + v.capacity();
	}

	vector& operator = (const vector& v)
	{
		swap(v);
		return *this;
	}

	~vector()
	{
		delete[] m_start;
		m_start = nullptr;
		m_finish = nullptr;
		m_endOfStorage = nullptr;
	}

	T& operator [] (int i)
	{
		return m_start[i];
	}

	const T& operator [] (int i) const
	{
		return m_start[i];
	}

	size_t size() const
	{
		return m_finish - m_start;
	}

	size_t capacity() const
	{
		return m_endOfStorage - m_start;
	}

	void reserve(size_t _size)
	{
		int _capacity = capacity();

		if (_size > _capacity)
		{
			if (_capacity == 0)
			{
				_capacity = 1;
			}
			while (_capacity < _size)
			{
				_capacity *= 2;
			}

			T* tmp = new T[_capacity];
			m_endOfStorage = tmp + _capacity;
			int OldSize = size();
			m_finish = tmp + OldSize;
			if (m_start != nullptr)
			{
				for (int i = 0; i < OldSize; i++)
				{
					tmp[i] = m_start[i];
				}
				delete[] m_start;
			}
			m_start = tmp;
		}
	}

	void resize(size_t _size, const T& value = T())
	{
		reserve(_size);

		for (int i = size(); i < _size; i++)
		{
			m_start[i] = value;
		}

		m_finish = m_start + _size;
	}

	iterator insert(iterator pos, const T &value)
	{
		int tmp = pos - m_start;
		reserve(size() + 1);
		pos = m_start + tmp;

		for (int i = size() - 1; i > pos - m_start; i--)
		{
			m_start[i + 1] = m_start[i];
		}

		pos[0] = value;

		m_finish++;

		return pos;
	}

	iterator insert(iterator pos, int n, const T &value)
	{
		int tmp = pos - m_start;
		reserve(size() + n);
		pos = m_start + tmp;

		for (int i = size() - 1; i > pos - m_start; i--)
		{
			m_start[i + n] = m_start[i];
		}

		for (int i = 0; i < n; i++)
		{
			pos[i] = value;
		}

		m_finish += n;

		return pos;
	}
	iterator insert(iterator pos, const T * start, const T * end)
	{
		int tmp = pos - m_start;
		int externsize = end - start;
		reserve(size() + externsize);
		pos = m_start + tmp;

		for (int i = size() - 1; i > pos - m_start; i--)
		{
			m_start[i + externsize] = m_start[i];
		}

		for (int i = 0; i < externsize; i++)
		{
			pos[i] = start[i];
		}

		m_finish += externsize;

		return pos;
	}

	iterator erase(iterator pos)
	{
		for (int i = pos - m_start; i < size() - 1; i++)
		{
			m_start[i] = m_start[i + 1];
		}

		m_finish--;

		return pos;
	}

	iterator erase(iterator start, iterator end)
	{
		int externsize = end - start;

		for (int i = start - m_start; i < size() - externsize; i++)
		{
			m_start[i] = m_start[i + externsize];
		}

		m_finish -= externsize;

		return start;
	}


	void push_back(const T& value)
	{
		reserve(size() + 1);
		*m_finish = value;
		m_finish++;
	}

	void pop_back()
	{
		m_finish--;
	}

private:
	iterator m_start;
	iterator m_finish;
	iterator m_endOfStorage;
};

};

Vector.cpp

#include 
#include "Vector.h"
using namespace std;

int main()
{

	int a[] = { 2, 3, 4, 5 };
	gwp::vector v2(a, a + 4);
	gwp::vector v1(v2.begin(), v2.end());

	gwp::vector v(1, 0);
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	v.push_back(4);
	v.push_back(5);
	v.push_back(6);
	v.push_back(7);
	v.push_back(8);
	v.push_back(9);
	v.pop_back();

	cout << v.size() << endl;
	cout << v.capacity() << endl;

	for (auto & e : v)
	{
		cout << e << " ";
	}
	cout << endl;

	v.insert(v.begin() + 2, a + 1, a + 3);

	v.erase(v1.begin() + 2, v1.end() - 1);

	for (int i = 0; i < v1.size(); i++)
	{
		cout << v1[i] << endl;
	}

	cout << v1.size() << endl;
	system("pause");
	return 0;
}

你可能感兴趣的:(vector类的模拟实现(包括vector的构造,拷贝构造, 赋值运算符重载,中括号运算符重载, 迭代器, 增删改查, 包括三种insert, 两种erase))