类模板案例
1.C++函数模板案例2.普通函数与函数模板调用规则3.普通函数与函数模板调用规则24.模板的局限性5.类模板6.类模板与函数模板的区别7.类模板中成员函数创建时机8.类模板对象做函数参数9.类模板与继承10.类模板成员函数类外实现11.类模板分文件编写12.类模板与友元
13.类模板案例
14.STL初识15.容器算法迭代器初识16.Vector中存放自定义数据类型17.Vector容器镶套容器18.STL---常用容器19.string赋值操作20.string字符串拼接21.string查找和替换22.string字符串比较/字符存取/插入和删除/子串获取23.vector容器/构造函数/赋值操作/容量和大小/插入和删除/数据存储/互换容器/预留空间24.deque容器/构造函数/赋值操作/大小操作/插入和删除/数据存取/排序25.案例--评委打分26.力扣---两数之和---unordered_map-STL容器案例描述:实现一个通用的数组类,要求如下:
可以对内置数据类型以及自定义数据类型进行存储
将数组中的数据存储到堆区
构造函数中可以传入数组的容量
提供对应的拷贝构造函数以及operator=防止浅拷贝问题
提供尾插法和尾删法对数组中的数据进行增加和删除
可以通过下标的方式访问数组中的元素
可以获取数组中当前元素的个数和数组的容量
//自己的通用的数组类 #pragma once #include <iostream> #include <string> 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拷贝构造调用"<<endl; this->m_Capacity=arr.m_Capacity; this->m_Size=arr.m_Size; //this->pAddress=arr.pAddress; //深拷贝 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) { cout<<"MyArray的operator=调用"<<endl; //先判断原来堆区是否有数据,如果有先释放 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--; } //通过下标方式访问数组中的元素 arr[0] =100 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<<"MyArray析构函数调用"<<endl; delete[] this->pAddress; this->pAddress=NULL; } } private: T * pAddress;//指针指向堆区开辟的真实数组 int m_Capacity;//数组容量 int m_Size;//数组大小 };
#include <iostream> using namespace std; #include <string> #include "MyArray.hpp" void printIntArray(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; printIntArray(arr1); cout<<"arr1的容量为:"<<arr1.getCapacity()<<endl; cout<<"arr1的大小为:"<<arr1.getSize()<<endl; MyArray<int> arr2(arr1); cout<<"arr2的打印输出:"<<endl; printIntArray(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_Name=name; this->m_Age=age; } 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("孙悟空",999); Person p2("韩信",30); Person p3("妲己",20); Person p4("赵云",25); Person p5("安其拉",27); //将数据插入到数组中 arr.Push_Back(p1); arr.Push_Back(p2); arr.Push_Back(p3); arr.Push_Back(p4); arr.Push_Back(p5); //打印数组 printPersonArray(arr); //输出容量 cout<<"arr的容量为:"<<arr.getCapacity()<<endl; //输出大小 cout<<"arr的大小为:"<<arr.getSize()<<endl; } int main() { test01(); test02(); return 0; }
总结:
能够利用所学知识点实现通用的数组
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)