黑马程序员C++教程从0到1入门编程16模板--数组类的封装
数组类的封装
**
实现一个通用数组类
(1)可以对内置数据类型以及自定义数据类型的数据进行储存
(2)将数组中的数据储存到堆区
(3)构造函数中可以传入数组的容量
(4)提供对应的拷贝构造函数以及operator=防止浅拷贝问题
(5)提供尾插法和尾删法对数组中的数据进行增加和删除
(6)可以通过下标的方式访问数组中的元素
(7)可以获得数组当前元素个数和数组容量
**
mayarray.hpp文件
//自己通用的数组类
#pragma once
#include <iostream>
using namespace std;
template<class T>
class MyArray
{
public:
//有参构造 参数 容量
MyArray(int capacity)
{
//cout << "myarray有参构造调用" << endl;
this->m_Capacity = capacity;
this->m_Size = 0;
this->pAddress = new T[this->m_Capacity];
}
//拷贝构造
MyArray(const MyArray& arr)
{
//cout << "myarray的operator参构造调用" << endl;
this->m_Capacity = arr.m_Capacity;
this->m_Size = arr.m_Size;
//this->pAddress=arr.pAddress;
//深拷贝
this->m_Capacity = arr.m_Capacity;
this->m_Size = arr.m_Size;
this->pAddress = new T[arr.m_Capacity];
//将arr中的数据都拷贝过来
for (int i = 0;i < this->m_Size;i++)
{
this->pAddress[i] = arr.pAddress[i];
}
}
//operator =防止浅拷贝问题
MyArray& operator=(const MyArray& arr)
{
//判断原来堆区是否有数据,如果有先释放
if (this->pAddress != NULL)
{
delete[] this->pAddress;
this->pAddress = NULL;
this->m_Capacity = 0;
this->m_Size = 0;
}
//深拷贝
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)
{
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()
{
// cout << "myarray析构函数调用" << endl;
if (this->pAddress != NULL)
{
delete[] this->pAddress;
this->pAddress = NULL;
}
}
private:
T*pAddress;//指针指向堆区开辟真实的数组
int m_Capacity;//数组容量
int m_Size;//数组大小
};
main.cpp文件
#pragma once//防止头文件包含
#include<string>
#include<fstream>
#include <iostream>
#include"myArray.hpp"
using namespace std;
//类模板案例-数组封装的需求
#if 1
void printInArray(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 < 5;i++)
{
//利用尾插法向数组中插入数据
arr1.Push_Back(i);
}
cout << "arr1的打印输出为:" << endl;
printInArray(arr1);
cout << "arr1的容量:" <<arr1.getCapacity()<< endl;
cout << "arr1的大小:" <<arr1.getSize()<< endl;
MyArray <int>arr2(arr1);
cout << "arr1的打印输出为:" << endl;
printInArray(arr2);
//尾删
arr2.Pop_Back();
cout << "arr2的容量:" << arr2.getCapacity() << endl;
cout << "arr2的大小:" << arr2.getSize() << endl;
//MyArray <int>arr3(100);
//arr3 = arr1;
}
//测试自动以数据类型
class Person
{
public:
Person()
{
}
Person(string name,int age)
{
this->m_age = age;
this->m_name = name;
}
string m_name;
int m_age;
};
void printPersonArray(MyArray<Person>&arr)
{
for (int i = 0;i < arr.getSize(); i++)
{
cout << arr[i].m_name << " " << arr[i].m_age << endl;
}
}
void test02()
{
MyArray<Person>arr(10);
Person p1("张三", 19);
Person p2("张四", 11);
Person p3("张五", 12);
Person p4("张六", 14);
Person p5("张一", 13);
Person p6("张七", 15);
//将数据插入到数组中
arr.Push_Back(p1);
arr.Push_Back(p2);
arr.Push_Back(p3);
arr.Push_Back(p4);
arr.Push_Back(p5);
arr.Push_Back(p6);
//打印数组
printPersonArray(arr);
//输出容量
cout << arr.getSize() << endl;
//输出大小
cout << arr.getCapacity() << endl;
}
#endif // 1
int main()
{
test01();
test02();
//test03();
system("pause");
return 0;
}
本文介绍了一个使用C++模板实现的通用数组类,能够存储内置及自定义数据类型,具备深拷贝、尾插尾删等特性,适用于多种编程需求。
350

被折叠的 条评论
为什么被折叠?



