基于模板实现 MyArray 数组

基于模板实现 MyArray 数组



1. 基本要求

  • MyArray 数组需要支持内置数据类型与自定义数据类型
  • MyArray 数据部分存储在堆区
  • MyArray 提供构造函数的函数需要指定数组容量
  • MyArray 需要提供析构函数,释放堆区内存
  • MyArray 需要提供拷贝构造函数
  • MyArray 需要提供 operator=operator[] 重载操作符
  • MyArray 需要提供 push_back尾插函数与pop_back尾删函数
  • MyArray 需要提供接口获得元素个数 与 数组容量

2. 代码实现

// MyArray.hpp
template <class T>
class MyArray{
public:
    MyArray(int capacity);
    ~MyArray();
    MyArray(const MyArray &other);
    MyArray& operator=(const MyArray &other) ;
    void push_back(const T &t) ;
    void pop_back();
    T& operator[](int index);
    int size();
    int capacity();
private:
    int m_capacity;
    int m_size;
    T *m_pArr;
};

template<class T>
MyArray<T>::MyArray(int capacity):m_capacity{capacity}, m_size{0}{
    m_pArr = new T[m_capacity];
    cout << "构造函数..." << endl;
}

template<class T>
MyArray<T>::~MyArray(){
    if (m_pArr != nullptr) {
        delete[] m_pArr;
        m_pArr = nullptr;
    }
   cout << "析构函数..." << endl;
}

template<class T>
MyArray<T>::MyArray(const MyArray &other) {
    m_capacity = other.m_capacity;
    m_size = other.m_size;
    m_pArr = new T[m_capacity];
    // 将数据拷贝过来
    for(int i = 0; i < m_size; i++) {
        m_pArr[i] = other.m_pArr[i];
    }
    cout << "拷贝构造..." << endl;
}

template<class T>
MyArray<T>& MyArray<T>::operator=(const MyArray &other) {
    cout << " operator=() ..." << endl;
    if (this == &other)
        return *this;
    // 先判断原来堆区是否有数据,有数据则释放
    if (this->m_pArr != nullptr) {
        delete [] m_pArr;
        m_pArr = nullptr;
        m_capacity = 0;
        m_size = 0;
    }
    // 深拷贝
    m_capacity = other.m_capacity;
    m_size = other.m_size;
    m_pArr = new T[m_capacity];
    for(int i = 0; i < m_size; i++) {
        m_pArr[i] = other.m_pArr[i];
    }
    return *this;
}

template<class T>
void MyArray<T>::push_back(const T &t) {
    cout << "push_back() ..." << endl;
    if (m_size < m_capacity) {
        *(m_pArr + m_size) = t;
        m_size++;
    }
}
template<class T>
void MyArray<T>::pop_back() {
    cout << "pop_back()..." << endl;
    if (m_size > 0)
        m_size--;
    return;    
} 

template<class T>
T& MyArray<T>::operator[](int index){
    cout << "operator[]()..." << endl;
    if (index < 0)
        return *(m_pArr);
    else if (index < m_capacity)
        return *(m_pArr + index);
    else
        return *(m_pArr + m_capacity);
}

template<class T>
int MyArray<T>::size() {
    cout << "size() ..." << endl;
    return m_size;
}
template<class T>
int MyArray<T>::capacity() {
    cout << "capacity()..." << endl;
    return m_capacity;
}

3. 遇到问题

  • 使用模板实现时,头文件与实现文件分离在编译过程中会出现找不到函数实现的像。因此,基于模板实现的类一般将头文件与实现文件放在一个文件内,该文件一般保存为 xxx.hpp
  • operator= 返回值问题,在【C++ operator= 操作符重载返回值为什么是自身的引用?<–点击即可访问】详细介绍。

4. 代码测试

#include "myArray.hpp"

class Person{
public:
    Person() = default;
    Person(int age): m_age{age}{

    }
    Person operator=(const Person &p){
        m_age = p.m_age;
        return *this;
    }
    void dispaly() {
        cout << "age = " << m_age << endl;
    }
private:
    int m_age;
};


void test01() {
    Person p1(1);
    Person p2(2);
    Person p3(3);
    Person p4(p3);
    MyArray<Person> myArr(10);
    myArr.push_back(p1);
    myArr.push_back(p2);
    myArr.push_back(p3);
    myArr.pop_back();
    myArr.push_back(p4);

    cout << "The size of myArr is: " << myArr.size() << endl;
    cout << "The capacity of myArr is: " << myArr.capacity() << endl;

    MyArray<Person> myArr2(myArr);
    myArr = myArr2;

    MyArray<int> myIntArr(10);
    myIntArr.push_back(1);
    myIntArr.push_back(2);
    myIntArr.push_back(3);
    cout << myIntArr[0] << "\t" << myIntArr[1] << "\t" << myIntArr[2] << endl;

}
int main() {
    test01();
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值