基于模板实现 MyArray 数组
文章目录
基于模板实现 MyArray 数组1. 基本要求2. 代码实现3. 遇到问题4. 代码测试
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. 遇到问题
使用模板实现时,头文件与实现文件分离在编译过程中会出现找不到函数实现的像。因此,基于模板实现的类一般将头文件与实现文件放在一个文件内,该文件一般保存为 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;
}