zoukankan      html  css  js  c++  java
  • 自定义模板数组

    Person.h

    #pragma once
    #include <string>
    class Person
    {
    public:
        Person();
        Person(std::string name, int age) {
            this->mName = name;
            this->mAge = age;
        }
        ~Person();
    public:
        std::string mName;
        int mAge;
    };

    Person.cpp

    #include "stdafx.h"
    #include "Person.h"
    
    
    Person::Person()
    {
    }
    
    
    Person::~Person()
    {
    }

    MyArray.hpp

    #pragma once
    template <class T>
    class MyArray
    {
    public:
        explicit 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];
            for (int i = 0; i < arr.m_Size; i++)
            {
                this->pAddress[i] = arr.pAddress[i];
            }
        }
    
        T& operator[](int index)
        {
            return this->pAddress[index];
        }
    
        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--;
        }
    
        int getSize()
        {
            return this->m_Size;
        }
    
        ~MyArray()
        {
            if (this->pAddress != nullptr)
            {
                delete[] this->pAddress;
                this->pAddress = nullptr;
                this->m_Capacity = 0;
                this->m_Size = 0;
            }
        }
    private:
        T* pAddress;
        int m_Capacity;
        int m_Size;
    };

     main.cpp

    // Te03.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    
    #include "Person.h"
    #include "MyArray.hpp"
    
    void PrintArrayInt(MyArray<int>& arr)
    {
        for (int i = 0; i < arr.getSize(); i++)
        {
            cout << arr[i] << " ";
        }
        cout << endl;
    }
    
    
    void PrintPerson(MyArray<Person>& personArr)
    {
        for (int i = 0; i < personArr.getSize(); i++)
        {
            cout << "姓名:" << personArr[i].mName << "年龄:" << personArr[i].mAge << endl;
        }
    }
    
    void Test01()
    {
        MyArray<int> myArrayInt(10);
        for (int i = 0; i < 9; i++)
        {
            myArrayInt.Push_back(i);
        }
        myArrayInt.Push_back(100);
        PrintArrayInt(myArrayInt);
        myArrayInt.Pop_back();
        PrintArrayInt(myArrayInt);
    }
    
    void Test02()
    {
        MyArray<Person> myArrayPerson(10);
        Person p1("张飞", 25);
        Person p2("马超", 23);
        Person p3("赵云", 24);
        Person p4("关羽", 28);
        Person p5("黄忠", 76);
        myArrayPerson.Push_back(p1);
        myArrayPerson.Push_back(p2);
        myArrayPerson.Push_back(p3);
        myArrayPerson.Push_back(p4);
        myArrayPerson.Push_back(p5);
        PrintPerson(myArrayPerson);
    }
    
    int main()
    {
        Test01();
        cout << "------------------------------------" << endl;
        Test02();
        return 0;
    }
  • 相关阅读:
    使你的 Google Summer of Code 建议被接收的5个技巧
    洗牌算法
    自由--永不妥协
    Google Code Jam 2014 总结
    《神经网络与深度学习》(三) 稀疏编码
    《神经网络与深度学习》(二) 常用模型之自编码器
    《神经网络与深度学习》(四) 受限玻尔兹曼机
    《神经网络与深度学习》(五) 卷积神经网络CNN及tensorflow代码实现示例
    《神经网络与深度学习》(一) 感知机、多层神经网络、BP算法、深度学习
    语义哈希(semanticHashing)
  • 原文地址:https://www.cnblogs.com/mmc9527/p/10455019.html
Copyright © 2011-2022 走看看