【C++模板】模板实现通用的数组

简介: 【C++模板】模板实现通用的数组

案例描述: 实现一个通用的数组类

可以对内置数据类型以及自定义数据类型的数据进行存储

将数组中的数据存储到堆区

构造函数中可以传入数组的容量

提供对应的拷贝构造函数以及operator=防止浅拷贝问题

提供尾插法和尾删法对数组中的数据进行增加和删除

可以通过下标的方式访问数组中的元素

可以获取数组中当前元素个数和数组的容量

说明:首先写一个myArray.hpp代码,用于创建通用的数组。

#pragma once
#include<iostream>
using namespace std;

template<class T>
class MyArray
{


public:
  //构造函数 参数、容量
  MyArray(int capacity)
  {
    //cout << "正在调用有参构造" << endl;
    this->m_capacity = capacity;
    this->m_size = 0;
    //重新在堆区申请空间
    this->pAddress = new T[this->m_capacity];
  }
  //防止浅拷贝,写一个拷贝构造函数
  MyArray(const MyArray  &p)
  {
    //cout << "正在拷贝构造" << endl;
    this->m_capacity = p.m_capacity;
    this->m_size = p.m_size;
    //深拷贝
    this->pAddress = new T[p.m_capacity];;

    //将p中的数据拷贝过来
    for (int i = 0; i < p.m_size; i++)
      this->pAddress[i] = p.pAddress[i];
  }

  //防止浅拷贝另一个 : operator=
  MyArray& operator=(const MyArray & p)
  {
    //cout << "正在operate=构造" << endl;
    //先判断原来堆区是否有数据,如果有,先释放
    if (this->pAddress != NULL)
    {
      delete[] this->pAddress;
      this->m_capacity = 0;
      this->m_size = 0;
      this->pAddress = NULL;
    }
    //做深拷贝
    this->m_capacity = p.m_capacity;;
    this->m_size = p.m_size;
    this->pAddress = new T[p.m_capacity];
    //把原来数据传入尽量
    for (int i = 0; i < p.m_size; i++)
      this->pAddress[i] = p.pAddress[i];

    return *this;
  }


  //写一个尾插法
  void Push_back(const T &val)
  {
    if (this->m_capacity == this->m_size)
    {
      cout << "容器已经满" << endl;
      return;
    }
    this->pAddress[this->m_size] = val; //插入数据到尾部
    this->m_size++; //更新大小
  }
  //尾删法
  void Pop_back()
  {
    if (this->m_size == 0)
    {
      cout << "容器为空,不能再删除了" << endl;
      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)
    {
      //cout << "正在调用析构函数" << endl;
      delete[] this->pAddress;
      this->pAddress = NULL; //置空,防止野指针
    }
  }




private:
  int m_size;
  int m_capacity;
  T * pAddress;

};

测试效果:

void showPrint(MyArray<int> &p)
{
  for (int i = 0; i < p.getSize(); i++)
    cout << p[i] << " ";
  cout << endl;
}


void test()
{
  MyArray<int> p(5);
  /*MyArray<int>p1(p);
  MyArray<int> p2(100);
  p2 = p1;*/
  for (int i = 0; i < 5; i++)
  {
    p.Push_back(i+1);
  }
  cout << "打印数组中的数据:" << endl;
  showPrint(p);
  cout << "数组的容量为:" << p.getCapacity()<< endl;
  cout << "数组的大小为:" << p.getSize() << endl;

  cout << "删除尾元素:" << endl;
  p.Pop_back();
  cout << "删除后,打印数组中的数据:" << endl;
  showPrint(p);
  cout << "删除后,数组的容量为:" << p.getCapacity() << endl;
  cout << "删除后,数组的大小为:" << p.getSize() << endl;
}

举例子测试:

void printPersonArray(MyArray<Person> &personArr)
{
  for (int i = 0; i < personArr.getSize(); i++) {
    cout << "姓名:" << personArr[i].m_Name << " 年龄: " << personArr[i].m_Age << endl;
  }
}

void test01()
{
  //创建数组
  MyArray<Person> pArray(10);
  Person p1("孙悟空", 30);
  Person p2("韩信", 20);
  Person p3("妲己", 18);
  Person p4("王昭君", 15);
  Person p5("赵云", 24);

  //插入数据
  pArray.Push_back(p1);
  pArray.Push_back(p2);
  pArray.Push_back(p3);
  pArray.Push_back(p4);
  pArray.Push_back(p5);

  printPersonArray(pArray);

  cout << "pArray的大小:" << pArray.getSize() << endl;
  cout << "pArray的容量:" << pArray.getCapacity() << endl;

}

相关文章
|
3天前
|
存储 算法 编译器
C++的模板与泛型编程探秘
C++的模板与泛型编程探秘
9 0
|
4天前
|
编译器 C++
【C++从练气到飞升】08---模板
【C++从练气到飞升】08---模板
|
5天前
|
算法 编译器 C++
【C++入门到精通】新的类功能 | 可变参数模板 C++11 [ C++入门 ]
【C++入门到精通】新的类功能 | 可变参数模板 C++11 [ C++入门 ]
21 1
|
5天前
|
编译器 C语言 C++
【C++】模板进阶
【C++】模板进阶
13 1
|
6天前
|
存储 编译器 C++
【C++】内存管理和模板基础(new、delete、类及函数模板)
【C++】内存管理和模板基础(new、delete、类及函数模板)
21 1
|
11天前
|
C++
【期末不挂科-C++考前速过系列P6】大二C++实验作业-模板(4道代码题)【解析,注释】
【期末不挂科-C++考前速过系列P6】大二C++实验作业-模板(4道代码题)【解析,注释】
【期末不挂科-C++考前速过系列P6】大二C++实验作业-模板(4道代码题)【解析,注释】
|
12天前
|
存储 算法 C++
详解C++中的STL(标准模板库)容器
【4月更文挑战第30天】C++ STL容器包括序列容器(如`vector`、`list`、`deque`、`forward_list`、`array`和`string`)、关联容器(如`set`、`multiset`、`map`和`multimap`)和容器适配器(如`stack`、`queue`和`priority_queue`)。它们为动态数组、链表、栈、队列、集合和映射等数据结构提供了高效实现。选择合适的容器类型可优化性能,满足不同编程需求。
|
14天前
|
运维 Serverless Go
Serverless 应用引擎产品使用之在阿里云函数计算中c++模板,将编译好的C++程序放进去部署如何解决
阿里云Serverless 应用引擎(SAE)提供了完整的微服务应用生命周期管理能力,包括应用部署、服务治理、开发运维、资源管理等功能,并通过扩展功能支持多环境管理、API Gateway、事件驱动等高级应用场景,帮助企业快速构建、部署、运维和扩展微服务架构,实现Serverless化的应用部署与运维模式。以下是对SAE产品使用合集的概述,包括应用管理、服务治理、开发运维、资源管理等方面。
12 1
|
18天前
|
编译器 程序员 C++
C++从入门到精通:3.1模板编程——提高代码的复用性和灵活性
C++从入门到精通:3.1模板编程——提高代码的复用性和灵活性
|
4天前
|
设计模式 安全 算法
【C++入门到精通】特殊类的设计 | 单例模式 [ C++入门 ]
【C++入门到精通】特殊类的设计 | 单例模式 [ C++入门 ]
15 0