【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;

}

相关文章
|
8天前
|
搜索推荐 编译器 C语言
【C++核心】特殊的元素集合-数组与字符串详解
这篇文章详细讲解了C++中数组和字符串的基本概念、操作和应用,包括一维数组、二维数组的定义和使用,以及C风格字符串和C++字符串类的对比。
36 4
|
2月前
|
编译器 C++
【C++】——初识模板
【C++】——初识模板
32 1
【C++】——初识模板
|
4天前
|
存储 算法 程序员
C++ 11新特性之可变参数模板
C++ 11新特性之可变参数模板
11 0
|
29天前
|
C++
C++(十一)对象数组
本文介绍了C++中对象数组的使用方法及其注意事项。通过示例展示了如何定义和初始化对象数组,并解释了栈对象数组与堆对象数组在初始化时的区别。重点强调了构造器设计时应考虑无参构造器的重要性,以及在需要进一步初始化的情况下采用二段式初始化策略的应用场景。
|
2月前
|
算法 C++
c++学习笔记04 数组
这篇文章是C++学习笔记4,主题是数组。
38 4
|
2月前
|
编译器 C++
【C++】模板初级
【C++】模板初级
|
2月前
|
安全 编译器 C++
【C++】模板进阶
【C++】模板进阶
|
2月前
|
C++ 索引
C++数组、vector求最大值最小值及其下标
C++数组、vector求最大值最小值及其下标
60 0
|
2月前
|
并行计算 测试技术 开发工具
【简历模板】c/c++软件工程师
【简历模板】c/c++软件工程师
49 0
|
3月前
|
C++ 索引 运维
开发与运维数组问题之在C++中数组名和指针是等价如何解决
开发与运维数组问题之在C++中数组名和指针是等价如何解决
22 6