自定义数组
作者:互联网
我的初衷使用模板自定义一个数组,可以存储内置数据类型和自定义数据类型
首先,内置数据类型int测试是成功的
#include<iostream>
using namespace std;
template<class T>
class MyArray
{
public:
MyArray(int capacity)
{
cout << "有参构造调用" << endl;
this->m_capacity = capacity;
this->m_size = 0;
this->pAddress = new T[this->m_capacity];
}
MyArray(const MyArray& arr)
{
cout << "拷贝构造调用" << endl;
this->m_capacity = arr.m_capacity;
this->m_size = arr.m_size;
this->pAddress = new T[arr.m_capacity];
for (int i = 0; i < this->m_size; i++)
{
this->pAddress[i] = arr.pAddress[i];
}
}
MyArray& operator=(const MyArray& arr)
{
cout << "operator=调用" << endl;
//1.先判断原来堆区是否有数据,如果有,先释放
if (this->pAddress != NULL)
{
delete[] this->pAddress;
this->pAddress = NULL;
}
this->m_capacity = 0;
this->m_size = 0;
//2.深拷贝
this->m_capacity = arr.m_capacity;
this->m_size = arr.m_size;
this->pAddress = new T[arr.m_capacity];
for (int i = 0; i < this->m_size; i++)
{
this->pAddress[i] = arr.pAddress[i];
}
return *this;
}
//尾插法
void push_back(const T& val)
{
if (this->m_capacity == this->m_size)
{
cout << "数组已满,无法再继续插入数据" << endl;
return;
}
//在数组尾插入数据
this->pAddress[this->m_size] = val;
//更新数组大小
this->m_size++;
}
//尾删法
void pop_back()
{
//让他访问不到最后一个数据
if (this->m_size == 0) return;
this->m_size--;
}
//通过下标来访问数据
T& operator[](int index)
{
return this->pAddress[index];
}
//返回数组容量
int getCapacity()
{
return this->m_capacity;
}
//返回数组大小
int getsize()
{
return this->m_size;
}
~MyArray()
{
if (this->pAddress != NULL)
{
cout << "析构调用" << endl;
delete[] this->pAddress;
this->pAddress = NULL;
}
}
private:
//指针指向堆区开辟的真实数组
T* pAddress;
//数组容量
int m_capacity;
//数组大小
int m_size;
};
void PrintArray1(MyArray <int>& arr)
{
for (int i = 0; i < arr.getsize(); i++)
{
cout << arr[i] << endl;
}
}
void test01()
{
MyArray <int>arr1(5);
for (int i = 0; i < arr1.getCapacity(); i++)
{
arr1.push_back(i);
}
cout << "arr1打印输出为:" << endl;
PrintArray1(arr1);
}
int main()
{
test01();
system("pause");
return 0;
}
但当我自定义一个类person时却出了问题
我也没想明白错在哪里,可能基础知识掌握不牢吧,
代码我也附上,大家如果能找到问题所在,说明也理解了相关知识了,
也欢迎大家留言告诉我哪里出错了‘。
#include<iostream>
using namespace std;
template<class T>
class MyArray
{
public:
MyArray(int capacity)
{
cout << "有参构造调用" << endl;
this->m_capacity = capacity;
this->m_size = 0;
this->pAddress = new T[this->m_capacity];
}
MyArray(const MyArray& arr)
{
cout << "拷贝构造调用" << endl;
this->m_capacity = arr.m_capacity;
this->m_size = arr.m_size;
this->pAddress = new T[arr.m_capacity];
for (int i = 0; i < this->m_size; i++)
{
this->pAddress[i] = arr.pAddress[i];
}
}
MyArray& operator=(const MyArray& arr)
{
cout << "operator=调用" << endl;
//1.先判断原来堆区是否有数据,如果有,先释放
if (this->pAddress != NULL)
{
delete[] this->pAddress;
this->pAddress = NULL;
}
this->m_capacity = 0;
this->m_size = 0;
//2.深拷贝
this->m_capacity = arr.m_capacity;
this->m_size = arr.m_size;
this->pAddress = new T[arr.m_capacity];
for (int i = 0; i < this->m_size; i++)
{
this->pAddress[i] = arr.pAddress[i];
}
return *this;
}
//尾插法
void push_back(const T& val)
{
if (this->m_capacity == this->m_size)
{
cout << "数组已满,无法再继续插入数据" << endl;
return;
}
//在数组尾插入数据
this->pAddress[this->m_size] = val;
//更新数组大小
this->m_size++;
}
//尾删法
void pop_back()
{
//让他访问不到最后一个数据
if (this->m_size == 0) return;
this->m_size--;
}
//通过下标来访问数据
T& operator[](int index)
{
return this->pAddress[index];
}
//返回数组容量
int getCapacity()
{
return this->m_capacity;
}
//返回数组大小
int getsize()
{
return this->m_size;
}
~MyArray()
{
if (this->pAddress != NULL)
{
cout << "析构调用" << endl;
delete[] this->pAddress;
this->pAddress = NULL;
}
}
private:
//指针指向堆区开辟的真实数组
T* pAddress;
//数组容量
int m_capacity;
//数组大小
int m_size;
};
void PrintArray1(MyArray <int>& arr)
{
for (int i = 0; i < arr.getsize(); i++)
{
cout << arr[i] << endl;
}
}
void test01()
{
MyArray <int>arr1(5);
for (int i = 0; i < arr1.getCapacity(); i++)
{
arr1.push_back(i);
}
cout << "arr1打印输出为:" << endl;
PrintArray1(arr1);
}
class person
{
public:
person(string name, int age)
{
this->m_name = name;
this->m_age = age;
}
string m_name;
int m_age;
};
void PrintArray2(MyArray <person>& arr)
{
for (int i = 0; i < arr.getsize(); i++)
{
cout << "姓名:" << arr[i].m_name << "\t年龄:" << arr[i].m_age << endl;
}
}
void test02()
{
MyArray<person>arr2(2);
for (int i = 0; i < arr2.getCapacity(); i++)
{
person p("万能人", i);
arr2.push_back(p);
}
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
标签:arr,capacity,自定义,pAddress,MyArray,int,数组,size 来源: https://blog.csdn.net/qq_64360901/article/details/123120761