(二)自定义 STL vector模板类 —— MyVector 功能定义

作者的话:我怕我追不上那个被寄予厚望的自己,所有我想努力奔跑


如果没有看过 MyVector功能声明,则点击(一)自定义 vector模板类 —— MyVector 功能声明

MyVector 方法实现

在同一文件中实现,可以在每个方法前加上 inline

构造、析构实现:

// 构造一个容器,元素个数为 size 
template <typename T>
inline CMyVector<T>::CMyvector(int size):
    nSize(size), nCapacity(size), pBuff(nullptr)
{
    pBuff = new T[nSize];		// 开辟一段内存

    memset(pBuff, 0, sizeof(T) * size);		// 设置值   清空
}


// 构造一个容器,元素个数为 size的值为 elem
template <typename T>
inline CMyVector<T>::CMyVector(int size, const T& elem):
    nSize(size), nCapacity(size), pBuff(nullptr)
{
    pBuff = new T[nSize];

    for(int i = 0; i < size; i++)	// 为每一个元素赋值 elem
    {
        pBuff[i] = elem;
    }
}


// 复制构造
template <typename T>
inline CMyVector<T>::CMyVector(CMyVector<T> const& that):
    pBuff(nullptr)
{
    nSize = that.nSize;
    nCapacity = that.nCapacity;

    if(nCapacity != 0)		// 容器容量不能为 0
    {
        pBuff = new T[that.nCapacity];		// 大小要一样

	// 拷贝数据 
  	memcpy(pBuff, that.pBuff, sizeof(T) * that.nSize);
    }	
}


// 析构
template <typename T>
inline CMyVector<T>::~CMyVector()
{
    this->clear();		// 清空数据
}

运算符重载实现:

// 赋值重载
template <typename T>
inline CMyVector<T>& CMyVector<T>::operator=(CMyVector<T> const& that)
{
    if( this != &that)		// 不能自赋值
    {
       // 临时存储拷贝
       T* p_tem = new T[that.nCapacity];	
       memcpy(p_tmp, that.pBuff, sizeof(T) * that.nSize);

       clear();		// 清空原来的内存
       pBuff = p_tmp;	// 接管内存

       nSize = that.nSize;
       nCapacity = that.nCapacity;
    }

    return *this;
}


//  == 重载
template<typename T>
inline bool CMyVector::operator==(CMyVector<T> const& that)
{
    if(nSize != that.nSize)	// 长度不相等 直接false
        return false;

    for(size_t i = 0; i < nSize; i++)	// 判断每一个元素
    {
        if(pBuff[i] != that.pBuff[i])
             return false;
    }
 
    return true;
}


//  !=  重载
template <typename T>
inline bool CMyVector<T>::operator != (CMyVector<T> const& that)
{
    return !(*this == that);		//  代码重用
}


// []  重载
template <typename T>
inline T& CMyVector<T>::operator[](int index) const
{
     return pBuff[index];			// 不判断越界问题
}

无参功能方法实现:

// 释放内存
template<typename T>
inline void CMyVector<T>::clear()
{
    if(pBuff)			// 不为空  delete 释放内存
    {
        delete[] pBuff;
    }
    
    pBuff = nullptr;		// 可能会用到 指向 空

    nSize = 0;
    
    // 容量大小不需要改为 0
}


// 元素大小
template <typename T>
inline size_t CMyVector<T>::size() const		
{
    return nSize;
}


// 容量大小
template <typename T>
inline size_t CMyVector<T>::capacity() const
{
    return nCapacity;
}


// 判断是否为空
template <typename T>
inline bool CMyVector<T>::empty() const
{
    return nSiz == 0;
}
 
 
// 返回每一个元素
template <typename T>
inline T& CMyVector<T>::front()
{
    return pBuff[0];
}


// 返回最后一个元素
template <typename T>
inline T& CMyVector<T>::back()
{
    return pBuff[nSize - 1];
}

有参功能方法实现:

// 复制 n个elem 赋值给this   可以理解成 浴火重生
template <typename T>
inline void CMyVector<T>::assign(int n,const T& elem)
{
    clear();		// 清空原先的数据

    nSize = nCapacity = n;	// 重新设置数据
    pBuff = new T[n];		// 开辟 n个T型 内存

    for(size_t i = 0; i < n; i++)
    {
        pBuff[i] = elem;
    }
}


// 交换数据
template <typename T>
inline void CMyVector<T>::swap(CMyVector<T>& that)
{
    CMyVector<T> temp;

    temp = *this;	// 代码重用
    *this = that;
    that = temp;
}


// 扩充数组数组容量,不能改小
template <typename T>
inline void CMyVector<T>::reserve(int n)
{
    if(n > nCapacity)			// 判断 是否需要扩容
    {
        T* tempBuff = new T[n];		// 开辟一个临时内存
        
        memcpy(tempBuff, pBuff, sizeof(T) * nSize);  // 拷贝数据到临时内存中
      
        if(pBuff)			// 清空原先的内存数据
            delete[] pBuff;

        pBuff = tempBuff;		// 接管内存
        
        nCapacity = n;			// 容量大小为扩容的大小
    }
}


// 改变元素个数    改大用扩容	改小用 nSize = n
template <typename T>
inline void CMyVector<T>::resize(int n)
{
    if(n > nSize)		// 判断大小
    {
        reserve(n);		// 扩容

	// 设置后加的数据为 0
        memset(&pBuff[nSize], 0, sizeof(T) * (n - nSize));
    }

    nSize = n;		// 不管是大于数组容量   还是小于数据原本元素个数  
}


// 改变元素个数,多的值为 elem
inline void CMyVector<T>::resize(int n, const T& elem)		// 和上面的方法差不多
{
    if(n > nSize)
    {
        reverse(n);		// 扩容
       
        for(size_t i = nSize; i < n; i++)	// 后加的元素设置为 elem
        {
            pBuff[i] = elem;
        }
    }

    nSize = n;		
}


// 返回下标 index,越界抛出 out_of_range 异常
template <typename T>
inline T& CMyVector<T>::at(int index) const
{
    if(index >= 0 && index < nSize)	// 限制范围
    {
        return pBuff[index];	
    }

    throw "out_of_range";		// 越界  抛出异常
}


// 删除最后一个数据
template <typename T>
inline void CMyVector<T>::pop_back()
{
    if(nSize > 0)
    {
        nSize--;		// 告诉编译器,我不管了,你管去
    }
}


// 最重要的一个方法实现
// 从后面插入数据
template <typename T>
inline void CMyVector<T>::push_back(const T& elem)
{
    if( nSize == nCapacity)		// 容量不够了  告诉编译器 需要扩容了
        reserve(2 * nCapacity + 1);	// 此处扩容 两倍 + 1	+ 1 的原因是防止有0 的情况

    pBuff[nSize++] = elem;
}

到此处,我们的MyVector 已经完成的差不多了,可以在 主函数中正常的使用。 此文章就不在演示了。


提前祝大家新年快乐 ^ - ^

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章