c++实现类模板编写和容器嵌套容器--包括重载、深拷贝、Vector等语法

1.案例需求:
在这里插入图片描述
话不多说,直接上代码
MyArray.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 = new T[arr.m_capacity];
		//将arr中的数据都拷贝过来
		for (int i = 0; i < this->m_size; i++)
		{
			this->pAddress[i] = arr.pAddress[i];
		}
	}
	//operator= 防止浅拷贝问题
	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)
		{
			cout << "提示:容量已满,无法插入" << endl;
			return;
		}
		this->pAddress[this->m_size] = val;
		this->m_size++;
	}
	//尾删法
	void Del_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()
	{
		//cout << "MyArray的析构函数调用" << endl;
		if (this->pAddress != NULL)
		{
			delete[] this->pAddress;
			this->pAddress = NULL;
		}
	}

private:
	T* pAddress;//指针指向堆区开辟的真实数组

	int m_capacity;//数组容量

	int m_size;//数组大小
};

main.cpp

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

template<class T>
void printArray(MyArray<T>& 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);
	}
	printArray(arr1);
	cout << "arr1的容量:" << arr1.getCapacity()<<endl;
	cout << "arr1的大小:" << arr1.getSize() << endl;
	
	MyArray<int> arr2(arr1);//拷贝
	printArray(arr2);
	arr2.Del_Back();
	cout << "arr2尾删后:" << endl;
	cout << "arr2的容量:" << arr2.getCapacity() << endl;
	cout << "arr2的大小:" << arr2.getSize() << endl;
	//MyArray<int> arr3(100);

	//arr3 = arr2;//operator=
}

//测试自定义数据类型
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("孙悟空", 999);
	Person p2("韩信", 30);
	Person p3("妲己", 85);
	Person p4("后裔", 63);
	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();
	system("pause");
	return 0;
}

2.容器中嵌套容器,并将数据进行遍历输出

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

void test01()
{
	vector<vector<int>> v;
	vector<int> v1;
	vector<int> v2;
	vector<int> v3;
	vector<int> v4;
	for (int i = 0; i < 4; i++)
	{
		v1.push_back(i+1);
		v2.push_back(i+2);
		v3.push_back(i+3);
		v4.push_back(i+4);
	}
	v.push_back(v1);
	v.push_back(v2);
	v.push_back(v3);
	v.push_back(v4);

	//通过大容器,把所有数据遍历一次
	for (vector<vector<int>>::iterator it = v.begin(); it != v.end(); it++)
	{
		//(*it)----容器 vector<int>
		for (vector<int>::iterator it1 = (*it).begin(); it1 != (*it).end(); it1++)
		{
			cout << *it1 <<" ";
		}
		cout << endl;
	}
}
int main()
{
	test01();
	system("pause");
	return 0;
}

在这里插入图片描述

#include<iostream>
#include<ctime>
#include<string>
#include<list>

using namespace std;

class Person
{
public:
	Person(string name,int age,int height)
	{
		this->m_name = name;
		this->m_age = age;
		this->m_height = height;
	}

	string m_name;
	int m_age;
	int m_height;
};
//指定排序规则
bool relue(Person& p1, Person& p2)
{

	if (p1.m_age < p2.m_age)
	{
		return true;
	}
	else if (p1.m_age > p2.m_age)
	{
		return false;
	}
	else
	{
		return p1.m_height > p2.m_height;
	}
}
void test01()
{
	list<Person>l;
	Person p1("刘备", 35, 175);
	Person p2("曹操", 45, 180);
	Person p3("孙权", 40, 170);
	Person p4("赵云", 25, 190);
	Person p5("张飞", 35, 160);
	Person p6("关羽", 35, 200);

	l.push_back(p1);
	l.push_back(p2);
	l.push_back(p3);
	l.push_back(p4);
	l.push_back(p5);
	l.push_back(p6);
	
	cout << "排序前:" << endl;
	for (list<Person>::iterator it = l.begin(); it != l.end(); it++)
	{
		cout << "姓名:" << it->m_name << "\t年龄:" << it->m_age << "\t身高:" << it->m_height << endl;
	}
	//排序后
	cout << "---------------------------------" << endl;
	cout << "排序后:" << endl;
	l.sort(relue);
	for (list<Person>::iterator it = l.begin(); it != l.end(); it++)
	{
		cout << "姓名:" << it->m_name << "\t年龄:" << it->m_age << "\t身高:" << it->m_height << endl;
	}
	
}
int main()
{
	test01();
	system("pause");
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值