基于模板实现 MyArray 数组
1. 基本要求
- MyArray 数组需要支持内置数据类型与自定义数据类型
- MyArray 数据部分存储在堆区
- MyArray 提供构造函数的函数需要指定数组容量
- MyArray 需要提供析构函数,释放堆区内存
- MyArray 需要提供拷贝构造函数
- MyArray 需要提供
operator=
,operator[]
重载操作符 - MyArray 需要提供
push_back
尾插函数与pop_back
尾删函数 - MyArray 需要提供接口获得元素个数 与 数组容量
2. 代码实现
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. 遇到问题
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;
}