C++模拟Vector

本文档展示了如何实现一个模板化的自定义向量类`MyVector`,包括基本的增删查改操作,以及迭代器的定义和使用。类中实现了动态内存管理、插入、删除、拷贝构造函数等功能。此外,还提供了`resize`和`reserve`等方法来调整容量。迭代器支持了基本的赋值、比较和自增操作。
摘要由CSDN通过智能技术生成
#ifndef __MYVECOTOR_H__
#define __MYVECTOTR_H__
#include<iostream>
#include<assert.h>
using namespace std;
template<class T>
class MyVector
{
	T* m_pBuff;
	int len;
	int max_len;
	struct MyIterator
	{
		T* pIt;
		//但返回类成员的引用时,最好是const引用
		MyIterator& operator=(MyIterator const& srcIt)
		{
			pIt = srcIt.pIt;
			return *this;
		}
		bool operator!=(MyIterator const& srcIt)
		{
			return pIt != srcIt.pIt;
		}
		MyIterator& operator++()
		{
			pIt++;
			return*this;
		}
		T operator*()
		{
			return *pIt;
		}
		MyIterator& operator+(MyIterator const& srcIt)
		{
			MyIterator tempIt = *this;
			tempIt.pIt += srcIt.pIt;
			return tempIt;
		}
		int operator-(MyIterator const& srcIt)
		{
			return pIt - srcIt.pIt;
		}
	};
public:
	MyIterator begin()
	{
		MyIterator tempIt;
		tempIt.pIt = m_pBuff;
		return tempIt;
	}
	MyIterator end()
	{
		MyIterator tempIt;
		tempIt.pIt = m_pBuff+len;
		return tempIt;
	}
	MyIterator& insert(MyIterator& pos, T const& elem)
	{
		//先扩容
		if (len >= max_len)
		{
			max_len += (max_len >> 1) > 1 ? (max_len >> 1) : 1;
			T* tempBuff = new T[max_len];
			for (int i = 0; i < len; i++)
			{
				tempBuff[i] = m_pBuff[i];
			}
			if (m_pBuff)
				delete[]m_pBuff;
			m_pBuff = tempBuff;
		}
		//确定pos的位置,并通过索引来操作,因为重新扩容后,指针会失效
		int index = pos.pIt - m_pBuff;
		for (int i = len; i > index; i--)
		{
			m_pBuff[i] = m_pBuff[i - 1];
		}
		m_pBuff[index] = elem;
		len++;
		MyIterator tempIt;
		tempIt.pIt = m_pBuff + index;
		return tempIt;
	}
	MyIterator erase(MyIterator const& pos)
	{
		int index = pos.pIt - m_pBuff;
		for (int i = index; i < len; i++)
			m_pBuff[i] = m_pBuff[i + 1];
		len--;
		MyIterator tempIt;
		tempIt = m_pBuff + index;
		return tempIt;
	}
public:
	MyVector();
	//带参构造
	MyVector(int num);
	MyVector(int num, T const& elem);
	//如果类中带有指针变量,并且有动态分配内存,必须要有一个拷贝构造函数
	MyVector(MyVector const& other);
	~MyVector();
	void clear();
public:
	int size()const;
	int capacity()const;
	bool empty()const;
	bool operator==(MyVector const& strVector)const;
	bool operator!=(MyVector const& strVector)const;
	bool operator>(MyVector const& strVector)const;
	bool operator>=(MyVector const& strVector)const;
	bool operator<(MyVector const& strVector)const;
	bool operator<=(MyVector const& strVector)const;
public:
	void swap(MyVector& destVecor);//交换,不能加const
	void assign(int n, T const& elem);
	T at(int index);//唯一一个主动抛异常的函数
	T operator[](int index);
	void push_back(T const& elem);
	void pop_back();
	T front();
	T back();
	void resize(int num);
	void reserve(int n);
};
template<class T>
void MyVector<T>::reserve(int n)
{
	if (max_len < n)
	{
		max_len = n;
		T* tempBuff = new T[max_len];
		for (int i = 0; i < len; i++)
		{
			tempBuff[i] = m_pBuff[i];
		}
		if (m_pBuff)
			delete[]m_pBuff;
		m_pBuff = tempBuff;
	}
}
template<class T>
void MyVector<T>::resize(int num)
{
	if (num < 0)
		assert(NULL);
	if (max_len < num)//构造一个更大的,把原来的赋值过来
	{
		max_len = num;
		T* tempBuff = new T[max_len];
		for (int i = 0; i < len; i++)
		{
			tempBuff[i] = m_pBuff[i];
		}
		memset(&tempBuff[len], 0, sizeof(T) * (num - len));
		if (m_pBuff)
			delete[]m_pBuff;
		m_pBuff = tempBuff;
	}
	len = num;
}
template<class T>
T MyVector<T>::back()
{
	return m_pBuff[len - 1];
}
template<class T>
T MyVector<T>::front()
{
	return m_pBuff[0];
}
template<class T>
void MyVector<T>::pop_back()
{
	if(len>0)
		len--;
}
template<class T>
void MyVector<T>::push_back(T const&elem)
{
	if (len >= max_len)
	{
		max_len += (max_len >> 1) > 1 ? (max_len >> 1) : 1;
		T tempBuff = new T[max_len];
		for (int i = 0; i < len; i++)
			tempBuff[i] = m_pBuff[i];
		if(m_pBuff)
			delete[]m_pBuff;
		m_pBuff = tempBuff;
	}
	m_pBuff[len++] = elem;
}
template<class T>
T MyVector<T>::operator[](int index)
{
	return m_pBuff[index];
}
template<class T>
T MyVector<T>::at(int index)
{
	if (index < 0 || index >= len)
		throw "out_of_range";
	return m_pBuff[index];
}
template<class T>
void MyVector<T>::assign(int num, T const& elem)
{
	clear();
	len = max_len = num;
	m_pBuff = nullptr;
	if (num > 0)
	{
		m_pBuff = new T[len];
		for (int i = 0; i < len; i++)
			m_pBuff[i] = elem;
	}
}
template<class T>
void MyVector<T>::swap(MyVector & destVector)
{
	T tempBuff = m_pBuff;
	int tempLen = len;
	int tempMaxLen = max_len;
	m_pBuff = destVector.m_pBuff;
	len = destVector.len;
	max_len = destVector.max_len;
	destVector.m_pBuff = tempBuff;
	destVector.len = tempBuff;
	destVector.max_len = tempMaxLen;
}
template<class T>
bool MyVector<T>::operator<=(MyVector const& strVector)const
{
	return (operator==(strVector) && operator<(strVector));
}
template<class T>
bool MyVector<T>::operator<(MyVector const& strVector)const
{
	return !operator>(strVector);
}
template<class T>
bool MyVector<T>::operator>=(MyVector const& strVector)const
{
	return (operator==(strVector)&& operator>(strVector));
}
template<class T>
bool MyVector<T>::operator>(MyVector const& strVetcor)const
{
	int minLen = this->len < strVetcor.len ? this->len : strVetcor.len;
	for (int i = 0; i < minLen; i++)
	{
		if (this->m_pBuff[i] > strVetcor.m_pBuff[i])
			return true;
		else if (this->m_pBuff[i] < strVetcor.m_pBuff[i])
			return false;
	}
	//分为三种情况:两者一样长;this长;str长
	if (len == minLen)
		return false;
	return true;
}
template<class T>
bool MyVector<T>::operator!=(MyVector const& strVector)const
{
	return(*this == strVector);
}
template<class T>
bool MyVector<T>::operator==(MyVector const&strVector)const
{
	//自己和自己比较
	if (this == &strVector) return true;
	if (len != strVector.len)return false;
	for (int i = 0; i < len; i++)
	{
		if (m_pBuff[i] != strVector.m_pBuff[i])
			return false;
	}
	return true;
}
template<class T>
//const 修饰类成员函数,其目的是防止成员函数修改被调用对象的值
//如果我们不想修改一个调用对象的值,所有的成员函数都应当声明为 const 成员函数。
int MyVector<T>::size()const
{
	return len;
}
template<class T>
int MyVector<T>::capacity()const
{
	return max_len;
}
template<class T>
bool MyVector<T>::empty()const
{
	return len==0;
}
template<class T>
MyVector<T>::MyVector(MyVector const& other)
{
	len = other.len;
	max_len = other.max_len;
	m_pBuff = nullptr;
	if (max_len > 0)
	{
		m_pBuff = new T[max_len];
		for (int i = 0;i < len; i++)
			m_pBuff[i] = other.m_pBuff[i];
	}
}
template<class T>
MyVector<T>::MyVector(int num, T const& elem)
{
	if (num < 0)
		abort();
	len = max_len = num;
	m_pBuff = nullptr;
	if (num > 0)
	{
		m_pBuff = new T[len];
		for (int i = 0; i < len; i++)
			m_pBuff[i] = elem;
		//memset(m_pBuff, elem, sizeof(T) * len);
	}
}
template<class T>
MyVector<T>::MyVector(int num)
{
	if (num < 0)
		abort();
	len = max_len = num;
	m_pBuff = nullptr;
	if (num > 0)
	{
		m_pBuff = new T[len];
		memset(m_pBuff, 0, sizeof(T) * len);
	}
}
template<class T>
MyVector<T>::MyVector()
{
	m_pBuff = nullptr;
	len = max_len = nullptr;
}
template<class T>
MyVector<T>::~MyVector()
{
	clear();
}
template<class T>
void MyVector<T>::clear()
{
	if (m_pBuff)
		delete[]m_pBuff;
	m_pBuff = nullptr;
	len = max_len = 0;
}

#endif // !__MYVECOTOR_H__

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值