#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__
C++模拟Vector
最新推荐文章于 2022-09-08 16:22:56 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)