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

学习内容:

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

C++数组模板封装,主要成员包括: public: Array(); Array(int size); Array(int size, T value); Array(Array<T> & other); Array(T * arr, int size); Array(int size, T* arr, int arrSize); template <typename T2> Array(Array<T2> & other); template <typename T2> Array(T2 * arr, int size); template <typename T2> Array(int size, T2 * arr, int arrSize); ~Array(void); inline int Size(); // 返回元素的个数 inline int MemorySize(); // 返回实际占用内的字节数 inline void ReSize(int size); // 重新设定大小,清除所有的数据 inline T * Buffer(); // 获取内部数组的首地址 inline void SetValue(int index, T val); // 设置第index个数据的值 inline T GetValue(int index); // 获得第index个数据 T SquareOfNorm2(); // 2范式的平方 T SquareOfDistance(Array<T> & another); // 距离另一个数组的距离的平方 T Distance(Array<T> & another); // 计算两个数组的距离 Array<T> Cut(int start, int len); // 从向量中截取一部分成为新的向量 Array<T> Cut(int start); // 将数组剪切一部分,返回新的数组,从start开始,一直到结束 T Norm0(); // 0范式,即求非0元素个数 T Norm1(); // 1范式,即求所有元素的和 T Norm2(); // 2范式,即求所有元素的平方和开根号 T Norm(int p = 2); // p范式 T Sum(); // 所有元素的和 void SetSmallValueToZero(); // 将绝对值小于eps的数值设置为0 T AbsMin(); // 返回绝对值的最小值 T Min(); // 返回最小值 T AbsMax(); // 返回绝对值的最大值 T Max(); // 返回最大值 void Offset(T value); // 所有数据偏移value T Average(); // 获得所有数据的平均值 void AverageTo(T value = 0); // 将数据平均化到某个数值 T Variance(); // 返回方差 void GaussianWhiten(); // 高斯白化,使得数组平均值为0,方差为1 void AbsValues(); // 所有的元素取绝对值 bool InfinityCheck(T maxValue = 1E50); // 数据检查,看数组中是否有数据为无穷大 bool IsZero(); // 返回数据是否全部为0 void Clear(); // 将数据全部清0 void Normalization(); // 正规化,每个元素平方和为1 void SumAsOne(); // 是每个元素的和为1 void SetMinTo(T value); // 将小于value的元素全部设置为value void SetMaxTo(T value); // 将大于value的元素全部设置为value virtual void WriteToTextFile(string filename, bool append = false); virtual void WriteToTextFile(ofstream & o); virtual void ReadFromTextFile(string filename); virtual void ReadFromTextFile(ifstream & i); protected: inline void Init(int size); public: Array<T> & operator = (Array<T> & v); Array<T> & operator = (T value); T & operator[] (int index); template<typename T2> Array<T> & operator = (Array<T2> & v); template<typename T> friend ostream & operator << (ostream & out, Array<T> & vector); template<typename T> friend istream & operator >> (istream & in, Array<T> & vector);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值