このプログラムは、一般的な配列クラスを実装し
ます。プログラムの機能は、プログラムで確認できます。
注:作成者はclionを使用して書き込むため、
CMakeLists.txtファイルを書き込む必要があります。CMakeLists.txtファイルの内容は次のとおりです。次のとおりです。
project( class_template_case )
set(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_CXX_STANDARD 14)
add_executable(mian mian.cpp)p
最初にMyArray.hppファイルを作成し、このファイルに配列クラステンプレートを記述します。
注:
クラステンプレートのメンバー関数は呼び出しフェーズでのみ作成され、コンパイルおよびリンクできないサブファイル(.cppおよび.h)が生成されるため、(c ++および.h)を使用しないのはなぜですか
。ソースコードMyArray.hppのは次のとおりです
//
// Created by wenbo on 2020/10/28.
//
#ifndef CLASS_TEMPLATE_CASE_MYARRAY_HPP
#define CLASS_TEMPLATE_CASE_MYARRAY_HPP
#endif //CLASS_TEMPLATE_CASE_MYARRAY_HPP
#pragma once
#include <iostream>
#include <string>
using namespace std;
//定义类模板
//该类模板可以存放自己定义的数据类型
template<class T>
class MyArray
{
public:
//有参构造,初始化
MyArray(int capacity)
{
this->m_Capacity=capacity;//传入容量
this->m_Size=0;//初始化数组大小
this->PAddress= new T[this->m_Capacity];//开辟容量为m_Capacity的堆区,来存放数组
}
//利用拷贝构造和以及operator =重载 防止浅拷贝的问题
//拷贝构造
MyArray(const MyArray &arr)
{
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<arr.m_Size;i++)
{
this->PAddress[i]=arr.PAddress[i];
}
}
//operator = (重载)
MyArray& operator =(const MyArray &arr)//返回引用的原因是因为 可以把返回的对象作为左值,比如 a=b=c
{
//要先判断堆区是否有数据,有的话要清空
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;//一定要返回自身,这样才可以 像a=b=c这种方式赋值;
}
//尾插法 赋值
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)//如果size都是0,说明数组里面没有内容
{
return;
}
this->m_Size--;
}
//通过下标访问数组元素(重载运算符[]),如arr[100]要做左值,则需要加引用&
T & operator [] (int index)
{
return this->PAddress[index];
}
//返回数组容量
int getCapaticy()
{
return this->m_Capacity;
}
//返回数组大小
int getSize()
{
return this->m_Size;
}
//析构函数,释放堆区
~MyArray()
{
if(this->PAddress!=NULL)
{
delete this->PAddress;
this->PAddress=NULL;
this->m_Capacity=0;
this->m_Size=0;
}
}
//属性
private:
T* PAddress;//PAddress指向堆区开辟的数组
int m_Capacity;//数组容量
int m_Size;//数组大小
};
その後、main関数でtest01()とtest02()の2つのテストを行いました。1つはint型のデータで、もう1つはカスタムデータ型(クラス)です
。main.cppのソースコードは次のとおりです。
//
// Created by wenbo on 2020/10/28.
//
#include "MyArray.hpp"
#include<iostream>
#include <string>
using namespace std;
//输出int数据类型
void printperson(MyArray<int> &arr)
{
for(int i=0;i<arr.getSize();i++)
{
cout<<arr[i]<<endl;
}
}
void test01()
{
MyArray<int> arr(5);
//利用尾插赋值
arr.Push_Back(1);
arr.Push_Back(2);
arr.Push_Back(3);
arr.Push_Back(4);
arr.Push_Back(5);
//打印数组
printperson(arr);
cout<<"数组容量:"<<arr.getCapaticy()<<endl;
cout<<"数组大小:"<<arr.getSize()<<endl;
}
//自定义数据类型测试MyArray 这个模板类
class Person
{
public:
Person() {
};
Person(string name,int age)
{
this->m_Name=name;
this->m_Age=age;
}
string m_Name;
int m_Age;
};
//输出Person数据类型
void printperson(MyArray<Person> &arr)
{
for(int i=0;i<arr.getSize();i++)
{
cout<<"姓名:"<<arr[i].m_Name<<"age = "<<arr[i].m_Age<<endl;
}
}
void test02()
{
MyArray<Person> arr(5);
Person p1("sun",100);
Person p2("zhu",200);
Person p3("sha",300);
Person p4("tang",400);
Person p5("bai",500);
//利用尾插赋值
arr.Push_Back(p1);
arr.Push_Back(p2);
arr.Push_Back(p3);
arr.Push_Back(p4);
arr.Push_Back(p5);
//打印数组
printperson(arr);
cout<<"数组容量:"<<arr.getCapaticy()<<endl;
cout<<"数组大小:"<<arr.getSize()<<endl;
}
int main()
{
cout<<"****************test01()*********************"<<endl;
test01();
cout<<"****************test02()*********************"<<endl;
test02();
return 0;
}
操作の結果は次のとおりです。
****************test01()*********************
1
2
3
4
5
数组容量:5
数组大小:5
****************test02()*********************
姓名:sunage = 100
姓名:zhuage = 200
姓名:shaage = 300
姓名:tangage = 400
姓名:baiage = 500
数组容量:5
数组大小:5