Ejemplo de plantilla de clase: matriz general

Para implementar una clase de matriz general, los requisitos son los siguientes:

Puede almacenar tipos de datos integrados y tipos de datos personalizados

Almacene los datos en la matriz en el área del montón

Proporcione el constructor de copia y el operador correspondientes = para evitar problemas de copia superficial

Proporcione métodos de interpolación de cola y eliminación de cola para agregar y eliminar datos en la matriz

Puede acceder a los elementos de la matriz subindicando

Puede obtener la capacidad de la matriz y la cantidad de elementos actuales

Código de plantilla de clase: Array.hpp

#pragma once
#include <iostream>
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];
    }
    ~MyArray(){
        if(this->pAddress!=NULL) {
            delete[] this->pAddress;
            this->pAddress=NULL;
        }
    }
    //尾插入
    void Push_Back(const T &val) {
        if(this->m_capacity==this->m_size) return;
        this->pAddress[this->m_size++]=val;
    }
    //尾删除
    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(const MyArray& arr){
        this->m_capacity=arr.m_capacity;
        this->m_size=arr.m_size;
        this->pAddress=new T[this->m_capacity];
        for(int i=0;i<this->m_size;++i) {
            this->pAddress[i]=arr.pAddress[i];
        }
    }
    MyArray& operator = (const MyArray& arr){
        this->m_capacity=arr.m_capacity;
        this->m_size=arr.m_size;
        if(this->pAddress!=NULL) {
            delete[] this->pAddress;
            this->pAddress=NULL;
        }
        this->pAddress=new T[this->m_capacity];
        for(int i=0;i<this->m_size;++i) {
            this->pAddress[i]=arr.pAddress[i];
        }
        return *this;
    }
public:
    T *pAddress;
    int m_size;
    int m_capacity;
};

Código de prueba main.cpp

#include <iostream>
#include "Array.hpp"

using namespace std;
template<class NameType,class AgeType>
class Person{
public:
    Person(){

    }
    Person(NameType name,AgeType age){
        this->age=age;
        this->name=name;
    }
    void show() {
        cout<<age<<" "<<name<<endl;
    }
    NameType name;
    AgeType age;
};
void test() {
    MyArray<Person<string,int> >P(4);
    P.Push_Back(Person<string,int>{"小明",22});
    P.Push_Back(Person<string,int>{"李华",18});
    P[0].show();
    P[1].show();
    cout<<P.getSize()<<endl;
    cout<<P.getCapacity()<<endl;
}
int main()
{
    test();
    MyArray<int> a(100);
    cout<<a.getCapacity()<<endl;
    for(int i=0;i<50;i++) {
        a.Push_Back(i);
    }
    cout<<a.getSize()<<endl;
    cout<<a[45]<<endl;
    a[1]=a[44]=99;
    cout<<a[1]<<endl;
    a.Pop_Back();
    cout<<a.getSize()<<endl;
    return 0;
}

 

Supongo que te gusta

Origin blog.csdn.net/qq_44132777/article/details/112790562
Recomendado
Clasificación