IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> c++笔记 模板 数组类封装的需求分析 -> 正文阅读

[数据结构与算法]c++笔记 模板 数组类封装的需求分析

主函数

//构造函数(容量)  拷贝构造  operator=
//利用下标方式访问数组中元素
//尾插法  尾删法
//获取数组容量  获取数组大小
//析构
#include<iostream>
using namespace std;
#include"MyArray.hpp"

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

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

	for (int i = 0; i < 5; i++) {
		//利用尾插法向数组中插入数据
		arr1.Push_back(i);
	}
	cout << "arr1的打印输出为: " << endl;
	printIntArray(arr1);
	cout << "arr1的容量为: " << arr1.getCapacity() << endl;
	cout << "arr1的大小为: " << arr1.getSize() << endl;
	MyArray <int>arr2(arr1);
	cout << "arr2的打印输出为: " << endl;
	printIntArray(arr2);

	//尾删
	arr2.Pop_Back();
	cout << "arr2尾删后: " << endl;
	cout << "arr2的容量为: " << arr2.getCapacity() << endl;
	cout << "arr2的大小为: " << arr2.getSize() << endl;

	//拷贝问题(深拷贝或者operator=)
	MyArray <int>arr3(100);
	arr3 = arr1;
}

//测试自定义数据类型
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 << " 年龄: " << arr[i].m_Age << endl;
	}
}


void test02() {
	MyArray<Person>arr(10);
	Person p1("孙策", 30);
	Person p2("周瑜", 28);
	Person p3("孙权", 22);
	Person p4("陆逊", 21);
	Person p5("吕蒙", 25);

	//将数据插入到数组中
	arr.Push_back(p1);
	arr.Push_back(p2);
	arr.Push_back(p3);
	arr.Push_back(p4);
	arr.Push_back(p5);

	//打印数组
	printPersonArray(arr);

	//输出容量与大小
	cout << "arr的容量为: " << arr.getCapacity() << endl;
	cout << "arr的大小为: " << arr.getSize() << endl;
}

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

头文件? .hpp

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

template<class T>
class MyArray {
public:
	//有参构造,传入初始容量
	MyArray(int capacity) {
		cout << "MyArray的有参构造调用" << endl;
		this->m_Capacity = capacity;
		this->m_Size = 0;
		this->pAddress = new T[this->m_Capacity];
	}
	//为防止浅拷贝,这里声明拷贝构造
	MyArray(const MyArray& arr) {
		cout << "MyArray的拷贝构造调用" << endl;
		this->m_Capacity = arr.m_Capacity;
		this->m_Size = arr.m_Size;
		//this->pAddress = arr.pAddrerss;  本句是浅拷贝问题所在,堆区内存重复释放

		//深拷贝
		this->pAddress = new T[arr.m_Capacity];

		//将arr中数据拷贝过来(如果arr内部原来有着数据)
		for (int i = 0; i < this->m_Size; i++) {
			this->pAddress[i] = arr.pAddress[i];
		}
	}
	//operate=  防止浅拷贝问题
	MyArray& operator=(const MyArray& arr) {
		cout << "MyArray的 operator= 构造调用" << endl;
		//先判断原来堆区是否有数据,如果有先释放
		//为防止原来储存的数据变成垃圾丢失,找不到了
		if (this->pAddress != NULL) {
			delete[] this->pAddress;
			this->pAddress = NULL;
			this->m_Capacity = 0;
			this->m_Size = 0;
		}

		//深拷贝
		this->m_Capacity = arr.m_Capacity;
		this->m_Size = arr.m_Size;
		this->pAddress = new T[arr.m_Capacity];
		for (int i = 0; i < this->m_Size; i++) {
			this->pAddress[i] = arr.pAddress[i];
		}
		return *this;
	}
	//尾插法
	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--;
	}
	//通过下标方式访问数组中的元素  arr[0]
	//arr是自定义数据类型(对象),使用数组元素的[]是无法被编译器识别的
	//必须使用运算符重载  operator[]

	//如果想使T返回值作为一个引用存在,需要返回T的引用     
	//左值:即数值可以被更改,且更改影响该数值的最根本值左值
	//不加引用是创建一个新的对象

	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 << "MyArray的析构构造调用" << endl;
			delete[] this->pAddress;
			this->pAddress = NULL;
		}
	}
private:
	T* pAddress;   //指针指向堆区开辟的真实数组
	int m_Capacity;    //数组容量
	int m_Size;     //数组大小
};

  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2021-10-08 12:01:22  更:2021-10-08 12:02:39 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/17 10:02:02-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码