首页 > 其他分享 >动态数组类及其模板

动态数组类及其模板

时间:2024-03-24 19:14:11浏览次数:29  
标签:Point int mallocSize DynamicArray 数组 array 动态 called 模板

先定义point类,再定义由point类的动态数组

#include <iostream>
#include <cassert>
using namespace std;

class Point {
private:
    int x, y;
public:
    Point() : x(0), y(0) {cout << "Point Default Constructor called." << endl;}
    Point(int x, int y) : x(x), y(y) {cout << "Point Constructor called." << endl;}
    ~Point() { cout << "Point Destructor called." << endl; }
    int getX() const { return x; }
    int getY() const { return y; }
    void move(int newX, int newY) {
        x = newX;
        y = newY;
    }
    void show(){ cout << "x = " << x << " , y = " << y << endl;  }
    friend ostream& operator<<(ostream& os, const Point& p);
};

ostream& operator<<(ostream& os, const Point& p) {
    cout << "(" << p.x << "," << p.y << ")";
    return os;
}

class ArrayOfPoints { //动态数组类
private:
    Point *points; //指向动态数组首地址
    int size; //数组大小
public:
    ArrayOfPoints(int size) : size(size) {
        points = new Point[size];
        cout << "ArrayOfPoints Constructor called." << endl;
    }
    ~ArrayOfPoints() {
        cout << "Deleting...ArrayOfPoints Destructor called." << endl;
        delete[] points;
    }

    Point& element(int index) {
        assert(index >= 0 && index < size);
        return points[index];
    }

    Point &operator[](int index) {
        assert(index >= 0 && index < size);
        return points[index];
    }
};

int main() {
    ArrayOfPoints points(10); //创建数组对象
    //points.element(0).move(5, 0); //访问数组元素的成员
    //points.element(1).move(15, 20); //访问数组元素的成员
    points[0].show();
    points[2].move(3,4);
    points[2].show();
    cout << points[2] << endl;
    return 0;
}

这个是动态数组类模板

#include <cstring>
#include <iostream>
using namespace std;

class Point {
private:
    int x, y;
public:
    Point(int _x = 0, int _y = 0);// constructor
    ~Point();// deconstructor
    friend ostream& operator<<(ostream& os, const Point& p);// cout <<  overload
    Point(const Point &p){
        x = p.x;y = p.y;
        cout << "Point copy constructor" << endl;
    }
};

Point::Point(int _x, int _y) {
    this->x = _x;
    this->y = _y;
    cout << "Point is called!" << endl;
}

Point::~Point() { cout << "~Point is called!" << endl; }

ostream& operator<<(ostream& os, const Point& p) {
    cout << "(" << p.x << "," << p.y << ")";
    return os;
}

template <typename T>
class DynamicArray {
private:
    T* array;                 // pointer
    unsigned int mallocSize;  // the length of dynamic array
public:
    //Constructors// mallocSize=length; 设置每个元素的初始内容是 content;
    DynamicArray(unsigned length, const T &content) ;
    //Copy Constructor
    DynamicArray(const DynamicArray<T> & anotherDA ) ;
    // Destructors
    ~DynamicArray();

    unsigned int capacity() const;

    //自己定义个operator[]  const 重载
    T& operator[](unsigned int i) ;
    //自己定义个 operator = 重载
    DynamicArray<T>& operator=(const DynamicArray<T>& anotherDA);
};

template <typename T>
DynamicArray<T>::DynamicArray(unsigned length, const T& content) {
    this->mallocSize = length;
    this->array = new T[this->mallocSize];

    cout << "new T[" << this->mallocSize << "] malloc " << this->mallocSize
         << "*" << sizeof(T) << "=" << this->mallocSize * sizeof(T)
         << " bytes memory in heap" << endl;

    for (int i = 0; i < length; ++i)  {this->array[i] = content;}
};

template <typename T>
DynamicArray<T>::~DynamicArray() {
    cout << "delete[] array free " << this->mallocSize << "*" << sizeof(T)
         << "=" << this->mallocSize * sizeof(T) << " bytes memory in heap" << endl;
    delete[] array;
};

template <typename T>
DynamicArray<T>::DynamicArray(const DynamicArray<T>& anotherDA) {
    cout  << "DynamicArray Copy Constructor is called" << endl;
    this->mallocSize = anotherDA.mallocSize;
    this->array = new T[this->mallocSize];
    for (int i = 0; i < this->mallocSize; ++i)
        this->array[i] = anotherDA.array[i];
};

template <typename T>
DynamicArray<T>& DynamicArray<T>::operator=(const DynamicArray<T>& anotherDA) {
    cout << "DynamicArray operator = is called"<< endl;
    if (this == &anotherDA) return *this;//等号两边相等,地址都是一样的,就没必要继续复制了
    if (this->array) delete[] this->array; //如果this->array是非空有东西的,就先删除再重新new,赋值
    this->mallocSize = anotherDA.mallocSize;
    this->array = new T[this->mallocSize];
    for (int i = 0; i < this->mallocSize; ++i)
        this->array[i] = anotherDA.array[i];
    return *this;
}

template <typename T>
unsigned int DynamicArray<T>::capacity()  const {return this->mallocSize;}

template <typename T>
T& DynamicArray<T>::operator[](unsigned int i)  {return this->array[i];}

int main(){
    int length = 6,i;

    DynamicArray<Point> iarray(length,Point(3));
    cout << "111111111111111111111111111111" << endl;
    DynamicArray<Point> iarray2(iarray);
    cout << "222222222222222222222222222222" << endl;
    DynamicArray<Point> iarray3(iarray2);
    cout << "33333333333333333333333333333" << endl;
    cout << endl;

    for(i=0;i < length;i++)  {cout << iarray3[i] <<" ";}
    cout << endl;

    for(i=0;i < length;i++)  {iarray[i] = Point(i,i+1);}
    cout << "4444444444444444444444444444" << endl;

    iarray3 = iarray2;  //因为两个是不同的动态数组,所以要先delete再new,逐个赋值
    cout << "55555555555555555555555555555" << endl;
    iarray2 = iarray;
    cout << endl;

    for(i=0;i<length;i++) {
        cout << iarray3[i] <<" ";
    }
    cout << endl;
    cout << "66666666666666666666666666666" << endl;

    return 0;
}

结果

Point is called!
Point is called!
Point is called!
Point is called!
Point is called!
Point is called!
Point is called!
new T[6] malloc 6*8=48 bytes memory in heap
~Point is called!
111111111111111111111111111111
DynamicArray Copy Constructor is called
Point is called!
Point is called!
Point is called!
Point is called!
Point is called!
Point is called!
222222222222222222222222222222
DynamicArray Copy Constructor is called
Point is called!
Point is called!
Point is called!
Point is called!
Point is called!
Point is called!
33333333333333333333333333333

(3,0) (3,0) (3,0) (3,0) (3,0) (3,0)
Point is called!
~Point is called!
Point is called!
~Point is called!
Point is called!
~Point is called!
Point is called!
~Point is called!
Point is called!
~Point is called!
Point is called!
~Point is called!
4444444444444444444444444444
DynamicArray operator = is called
~Point is called!
~Point is called!
~Point is called!
~Point is called!
~Point is called!
~Point is called!
Point is called!
Point is called!
Point is called!
Point is called!
Point is called!
Point is called!
55555555555555555555555555555
DynamicArray operator = is called
~Point is called!
~Point is called!
~Point is called!
~Point is called!
~Point is called!
~Point is called!
Point is called!
Point is called!
Point is called!
Point is called!
Point is called!
Point is called!

(3,0) (3,0) (3,0) (3,0) (3,0) (3,0)
66666666666666666666666666666
delete[] array free 6*8=48 bytes memory in heap
~Point is called!
~Point is called!
~Point is called!
~Point is called!
~Point is called!
~Point is called!
delete[] array free 6*8=48 bytes memory in heap
~Point is called!
~Point is called!
~Point is called!
~Point is called!
~Point is called!
~Point is called!
delete[] array free 6*8=48 bytes memory in heap
~Point is called!
~Point is called!
~Point is called!
~Point is called!
~Point is called!
~Point is called!

 

标签:Point,int,mallocSize,DynamicArray,数组,array,动态,called,模板
From: https://www.cnblogs.com/uacs2024/p/18092834

相关文章

  • 【动态规划】【同余前缀和】【多重背包】[推荐]2902. 和带限制的子多重集合的数目
    本文涉及知识点动态规划汇总C++算法:前缀和、前缀乘积、前缀异或的原理、源码及测试用例包括课程视频C++算法:滑动窗口总结多重背包LeetCode2902.和带限制的子多重集合的数目给你一个下标从0开始的非负整数数组nums和两个整数l和r。请你返回nums中子多重集......
  • MyBatis3源码深度解析(二十一)动态SQL实现原理(二)动态SQL解析过程、#{}和${}的区别
    文章目录前言8.5动态SQL解析过程8.5.1SQL配置转换为SqlSource对象8.5.2SqlSource转换为静态SQL语句8.6#{}和${}的区别8.7小结前言在【MyBatis3源码深度解析(二十)动态SQL实现原理(一)动态SQL的核心组件】中研究了MyBatis动态SQL相关的组件,如SqlSource用于描述......
  • arguments 这种类数组如何遍历
    类数组对象所谓的类数组对象:拥有一个length属性和若干索引属性的对象举个例子:vararray=['name','age','sex'];vararrayLike={0:'name',1:'age',2:'sex',length:3}即便如此,为什么叫做类数组对象呢?那让我们从读写、获取长度、遍......
  • 【晴问算法】提高篇—动态规划专题—最长公共子序列
    题目描述现有两个字符串s1​​​​与s2​,求s1​​​​与s2​​​​的最长公共子序列的长度(子序列可以不连续)。输入描述第一行为字符串s1​​,仅由小写字母组成,长度不超过100;第一行为字符串s2​​​,仅由小写字母组成,长度不超过100。输出描述输出一个整数,表示最长公共......
  • 软件项目开发运用的全套文档模板(规格说明书、详细设计、测试计划、验收报告)
       前言:在软件开发过程中,文档资料是非常关键的一部分,它们帮助团队成员理解项目需求、设计、实施、测试、验收等各个环节,确保项目的顺利进行。以下是针对您提到的各个阶段的文档资料概述:所有资料获取:点击获取开发阶段需求规格说明书:详细描述了软件系统的功能需求、非......
  • C++ 的标准模板库(STL)常用容器介绍
    C++的标准模板库(STL)提供了丰富的容器类来帮助开发者管理和存储数据。下面我将介绍C++中常用的STL容器,并且为每个容器提供一个简单的示例来说明其基本用法。1.vector(向量)#include<iostream>#include<vector>intmain(){std::vector<int>vec;//添加元......
  • 静态库、动态库比较与制作
    目录静态库、动态库比较与制作静态库与动态库比较静态库制作方法如何使用动态库地址回填静态库与动态库函数反汇编比较——延迟绑定动态库制作生成与位置无关的代码——-fPICgcc-share制作动态库.so编译ELF时,指定所用的动态库-l:指定库名-L:指定库路径动态库加载错误及解......
  • 安装Visual Studio2015后找不到C++项目模板解决办法
    安装VisualStudio2015后找不到C++项目模板解决办法:方法1:您可以通过修改VisualStudio来完成此操作,并且可以使用以下步骤完成此操作:1、转到“添加或删除程序”对话框中的“控制面板”;2、选择要修复的产品,然后单击“安装向导”,单击“Next;3、单击“repair。方法2:您可以通过以下......
  • Go数组的扩容规则
    Go数组的扩容规则Go数组的扩容规则技术要点先是双倍扩容,然后是一定的比例扩容,逐渐向1.25进行靠近在目前的实现里面,在小于256的时候会进行double,在大于256的时候,会根据一定的生长因子进行扩容,但是总体来说还是会逐渐的靠近到1.25funcgrowslice(et*_type,olds......
  • 一个操作让数组处理速度快了5倍,到底是为什么
     概述:通过对数组进行排序,代码更好地利用了缓存,从而提高了程序的性能。这种现象通常被称为"缓存友好"(cache-friendly)或"空间局部性"(spatiallocality)今天做一个数组数据计算时,发现一个效率问题,给大家分享一下一个数组排序和不排序时同样的逻辑处理速度是不一样的。排序后速度......