黑马程序员——数组类模板

MyArray.h

//自己的通用的数组类
#pragma once
#include<iostream>
using namespace std;

template<class T>
class MyArray {
private:
	T* pAddr;
	int m_Capacity;
	int m_Size;

public:
	//有参构造
	MyArray(int capacity) {
		//cout << "有参构造" << endl;
		this->m_Capacity = capacity;
		this->m_Size = 0;
		this->pAddr = new T[this->m_Capacity];
	}

	//拷贝构造
	MyArray(const MyArray& arr) {
		//cout << "拷贝构造" << endl;
		this->m_Capacity = arr.m_Capacity;
		this->m_Size = arr.m_Size;
		//深拷贝
		this->pAddr = new T[this->m_Capacity];
		for (int i = 0; i < this->m_Size; i++) {
			this->pAddr[i] = arr.pAddr[i];
		}
	}

	//operator=
	MyArray& operator=(const MyArray& arr) {
		//cout << "深拷贝" << endl;
		//先判断原来是否有数据
		if (this->pAddr != NULL) {
			delete[] this->pAddr;
			this->pAddr = NULL;
			this->m_Capacity = 0;
			this->m_Size = 0;
		}

		this->m_Capacity = arr.m_Capacity;
		this->m_Size = arr.m_Size;
		//深拷贝
		this->pAddr = new T[this->m_Capacity];
		for (int i = 0; i < this->m_Size; i++) {
			this->pAddr[i] = arr.pAddr[i];
		}

		return *this;
	}

	//尾插法
	void Push_Back(const T& val) {
		//判断容量和大小
		if (this->m_Capacity == this->m_Size) {
			cout << "数组已经满了" << endl;
			return;
		}

		this->pAddr[this->m_Size] = val;
		this->m_Size++;
	}

	//尾删法
	void Pop_Back() {
		if (this->m_Size == 0) {
			cout << "数组为空" << endl;
			return;
		}

		this->m_Size--;
	}

	//通过下标访问	a[0] = 99
	T& operator[](int index) {
		return this->pAddr[index];
	}

	//返回容量
	int GetCapacity() {
		return this->m_Capacity;
	}

	//返回大小
	int GetSize() {
		return this->m_Size;
	}

	~MyArray() {
		if (this->pAddr != NULL) {
			//cout << "析构函数" << endl;
			delete[] this->pAddr;
			this->pAddr = NULL;
		}
	}
};

数组类模板.cpp

#include <iostream>
using namespace std;
#include <string>
#include  "MyArray.h"

void printArray(MyArray<int>& arr) {
	for (int i = 0; i < arr.GetSize(); i++) {
		cout << arr[i] << endl;
	}
}

void test01() {
	MyArray<int>arr(5);

	//利用好尾插法
	for (int i = 0; i < 5; i++) {
		arr.Push_Back(i);
	}

	cout << "arr的打印输出为:" << endl;
	
	printArray(arr);

	cout << "arr的容量:" << arr.GetCapacity() << endl;
	cout << "arr的大小:" << arr.GetSize() << endl;

	MyArray <int>arr1(arr);
	printArray(arr1);
	//尾删
	arr1.Pop_Back();
	cout << "arr1尾删后" << endl;
	cout << "arr1的容量:" << arr1.GetCapacity() << endl;
	cout << "arr1的大小:" << arr1.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) {
	for (int i = 0; i < arr.GetSize(); i++) {
		cout << "姓名:" << arr[i].m_name << "\t年龄:" << arr[i].m_age << endl;
	}
}

void test02() {
	MyArray<Person>arr(10);

	Person p1("孙悟空", 555);
	Person p2("红孩儿", 555);
	Person p3("猪八戒", 555);
	Person p4("唐三藏", 555);
	Person p5("沙悟净", 555);
	Person p6("白骨精", 555);
	Person p7("女儿国", 555);
	Person p8("白龙马", 555);
	Person p9("史蒂夫", 555);
	Person p10("甘道夫", 555);

	//将数据插入数组
	arr.Push_Back(p1);
	arr.Push_Back(p2);
	arr.Push_Back(p3);
	arr.Push_Back(p4);
	arr.Push_Back(p5);
	arr.Push_Back(p6);
	arr.Push_Back(p7);
	arr.Push_Back(p8);
	arr.Push_Back(p9);
	arr.Push_Back(p10);

	PrintPersonArray(arr);
	cout << "arr的容量:" << arr.GetCapacity() << endl;
	cout << "arr的大小:" << arr.GetSize() << endl;

}

int main() {
	//test01();
	test02();

	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
黑马程序员的tb_brand是指在JavaWeb基础教程中创建的一个表。这个表是用来存储品牌信息的,具体的表结构和数据类型需要和JavaBean类中的成员变量保持一致。\[1\]在这个教程中,使用了Maven来构建项目,并且使用了MyBatis作为持久层框架,通过配置pom.xml文件来引入相关依赖。\[2\] Maven是一个用于管理和构建Java项目的工具,它提供了一套标准化的项目结构、构建流程和依赖管理机制。\[3\] #### 引用[.reference_title] - *1* [【JAVAWEB开发】黑马程序员java web案例资料(含Element的删除与修改)](https://blog.csdn.net/aasd23/article/details/126940147)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [黑马程序员-MyBatis 框架-最全入门笔记、阿伟看了都得说真大、真细、真全!!!](https://blog.csdn.net/qq_57383364/article/details/128103058)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [JavaWeb——黑马程序员课程笔记](https://blog.csdn.net/King_ZACC/article/details/128573804)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值