封装一个数组类模板( 数组中可以存任意类型数据,Linux实现)

学习内容:

案例描述: 实现一个通用的数组类,要求如下:
1.可以对内置数据类型以及自定义数据类型的数据进行存储
2.将数组中的数据存储到堆区
3.构造函数中可以传入数组的容量
4.提供对应的拷贝构造函数以及operator防止浅拷贝问题
5.提供尾插法和尾删法对数组中的数据进行增加和删除
6.可以通过下标的方式访问数组中的元素
7.可以获取数组总当前元素个数和数组的容量
原文链接:https://blog.csdn.net/XiaoQiu__/article/details/123078459

创建 myarray.hpp

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];//在堆区开辟数组空间
        }

        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 < 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];
            //拷贝arr中的数据
            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()
        {
            if (this->paddress!=NULL)
            {
                delete[]this->paddress;            
                this->paddress = NULL;
            }
        }

    private:
        T * paddress;  //指针指向堆区开辟的真实数组
        int m_capacity;//数组容量
        int m_size;    //数组大小
};                                      

创建类模板案例-数组类封装.cpp

代码如下:

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
using namespace std;
#include "myarray.hpp"

void printIntArray(myarray <int> & arr1)
{
    for (int i = 0; i < arr1.getSize(); i++)
    {
        cout << arr1[i]<<" ";
    }
    cout << endl;
}
void test()
{
    myarray <int> arr1(10);

    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 << "尾删后:" << endl;
    cout << "arr2的容量为:" << arr2.getcapacity() << endl;
    cout << "arr2的大小为:" << arr2.getSize() << endl;
}
//测试自定义数据类型
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)
{
    cout << endl;
    for (int i = 0; i < arr.getSize(); i++)
    {
        cout << " 姓名: " << arr[i].m_Name << " 年龄: " << arr[i].m_Age << endl;
    }
}

void test1()
{
    myarray<Person> arr(10);

    Person p1("小明", 2);
    Person p2("小张", 5);
    Person p3("小虎", 4);
    Person p4("小红", 2);
    Person p5("小赵", 4);

    //将数据插入到数组中
    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()
{
    test();
    test1();

    return 0;
}                                                

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值