利用Cpp类模板知识,实现一个通用的数组类

  • 可以对内置数据类型以及自定义数据类型的数据进行存储
  • 将数组中的数据存储到堆区
  • 构造函数中可以传入数组的容量
  • 提供对应的拷贝构造函数以及operator=防止浅拷贝问题
  • 提供尾插法和尾删法对数组中的数据进行增加和删除
  • 可以通过下标的方式访问数组中的元素
  • 可以获取数组中当前元素个数和数组的容量

myArray.hpp中代码

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

template<class T>
class MyArray
{
public:
    
	//构造函数
	MyArray(int capacity)
	{
		this->m_Capacity = capacity;
		this->m_Size = 0;
		pAddress = new T[this->m_Capacity];
	}

	//拷贝构造
	MyArray(const MyArray & arr)
	{
		this->m_Capacity = arr.m_Capacity;
		this->m_Size = arr.m_Size;
		this->pAddress = new T[this->m_Capacity];
		for (int i = 0; i < this->m_Size; i++)
		{
			//如果T为对象,而且还包含指针,必须需要重载 = 操作符,因为这个等号不是 构造 而是赋值,
			// 普通类型可以直接= 但是指针类型需要深拷贝
			this->pAddress[i] = arr.pAddress[i];
		}
	}

	//重载= 操作符  防止浅拷贝问题
	MyArray& operator=(const MyArray& myarray) {

		if (this->pAddress != NULL) {
			delete[] this->pAddress;
			this->m_Capacity = 0;
			this->m_Size = 0;
		}

		this->m_Capacity = myarray.m_Capacity;
		this->m_Size = myarray.m_Size;
		this->pAddress = new T[this->m_Capacity];
		for (int i = 0; i < this->m_Size; i++) {
			this->pAddress[i] = myarray[i];
		}
		return *this;
	}

	//重载[] 操作符  arr[0]
	T& operator [](int index)
	{
		return this->pAddress[index]; //不考虑越界,用户自己去处理
	}

	//尾插法
	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--;
	}

	//获取数组容量
	int getCapacity()
	{
		return this->m_Capacity;
	}

	//获取数组大小
	int	getSize()
	{
		return this->m_Size;
	}


	//析构
	~MyArray()
	{
		if (this->pAddress != NULL)
		{
			delete[] this->pAddress;
			this->pAddress = NULL;
			this->m_Capacity = 0;
			this->m_Size = 0;
		}
	}

private:
	T * pAddress;  //指向一个堆空间,这个空间存储真正的数据
	int m_Capacity; //容量
	int m_Size;   // 大小
};

数组类封装.cpp中

#include "myArray.hpp"
#include <string>

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

//测试内置数据类型
void test01()
{
	MyArray<int> array1(10);
	for (int i = 0; i < 10; i++)
	{
		array1.Push_back(i);
	}
	cout << "array1打印输出:" << endl;
	printIntArray(array1);
	cout << "array1的大小:" << array1.getSize() << endl;
	cout << "array1的容量:" << array1.getCapacity() << endl;

	cout << "--------------------------" << endl;

	MyArray<int> array2(array1);
	array2.Pop_back();
	cout << "array2打印输出:" << endl;
	printIntArray(array2);
	cout << "array2的大小:" << array2.getSize() << endl;
	cout << "array2的容量:" << array2.getCapacity() << endl;
}

//测试自定义数据类型
class Person {
public:
	Person() {} 
		Person(string name, int age) {
		this->m_Name = name;
		this->m_Age = age;
	}
public:
	string m_Name;
	int m_Age;
};

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

}

void test02()
{
	//创建数组
	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;

}

int main() {

	//test01();

	test02();

	system("pause");

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 好的,我可以回答这个问题。以下是使用模板实现顺序表的游戏战绩管理系统的 C++ 代码: #include <iostream> #include <string> using namespace std; template <typename T> class SeqList { private: T* data; // 存储数据的数组 int length; // 当前长度 int maxSize; // 最大容量 public: SeqList(int size = 10) { // 构造函数 data = new T[size]; length = ; maxSize = size; } ~SeqList() { // 析构函数 delete[] data; } bool insert(int i, T x) { // 在第 i 个位置插入元素 x if (i < 1 || i > length + 1 || length == maxSize) { return false; } for (int j = length; j >= i; j--) { data[j] = data[j - 1]; } data[i - 1] = x; length++; return true; } bool remove(int i) { // 删除第 i 个位置的元素 if (i < 1 || i > length) { return false; } for (int j = i; j < length; j++) { data[j - 1] = data[j]; } length--; return true; } int search(T x) { // 查找元素 x 的位置 for (int i = ; i < length; i++) { if (data[i] == x) { return i + 1; } } return ; } T get(int i) { // 获取第 i 个位置的元素 if (i < 1 || i > length) { return NULL; } return data[i - 1]; } int size() { // 获取当前长度 return length; } }; int main() { SeqList<string> gameRecord(100); // 创建一个最大容量为 100 的字符串类型的顺序表 gameRecord.insert(1, "Player1: 100 points"); // 在第 1 个位置插入元素 gameRecord.insert(2, "Player2: 80 points"); // 在第 2 个位置插入元素 gameRecord.insert(3, "Player3: 60 points"); // 在第 3 个位置插入元素 gameRecord.remove(2); // 删除第 2 个位置的元素 cout << "Game Record:" << endl; for (int i = 1; i <= gameRecord.size(); i++) { cout << gameRecord.get(i) << endl; // 获取第 i 个位置的元素并输出 } return ; } ### 回答2: 模板类是一种在C++中使用的通用编程工具,可以在不同类型参数下生成多个类的定义。使用模板实现顺序表的游戏战绩管理系统,可以实现对任意数据类型的游戏战绩进行管理。 首先,我们需要定义一个模板类,命名为GameRecord,用于存储游戏战绩的信息。该类包含成员变量playerName(玩家名称)和score(游戏得分)等。 ```c template <class T> class GameRecord { private: T* playerName; // 玩家名称 T* score; // 游戏得分 int capacity; // 容量 int size; // 元素个数 public: GameRecord(int capacity) { // 构造函数,初始化属性 this->capacity = capacity; playerName = new T[capacity]; score = new T[capacity]; size = 0; } ~GameRecord() { //析构函数,释放内存 delete[] playerName; delete[] score; } void addRecord(T name, T score) { // 添加游戏记录 if (size < capacity) { playerName[size] = name; this->score[size] = score; size++; } } void displayRecord() { //显示游戏记录 for (int i = 0; i < size; i++) { cout << "玩家:" << playerName[i] << ",得分:" << score[i] << endl; } } }; ``` 通过以上的模板类,我们可以根据具体的游戏战绩数据类型来实例化一个游戏战绩管理系统。例如,我们可以使用`GameRecord<string>`来管理字符串类型的玩家名称和游戏得分,也可以使用`GameRecord<int>`来管理整数类型的玩家名称和游戏得分。 ```c int main() { GameRecord<string> gameRecord(100); // 实例化一个管理字符串类型游戏战绩的对象 gameRecord.addRecord("小明", "100"); // 添加游戏记录 gameRecord.addRecord("小红", "200"); gameRecord.displayRecord(); // 显示游戏记录 GameRecord<int> gameRecord2(100); // 实例化一个管理整数类型游戏战绩的对象 gameRecord2.addRecord(1, 100); // 添加游戏记录 gameRecord2.addRecord(2, 200); gameRecord2.displayRecord(); // 显示游戏记录 return 0; } ``` 以上示例代码演示了使用模板实现顺序表的游戏战绩管理系统的基本操作,即添加游戏记录和显示游戏记录。您可以根据实际需求,对模板类的功能进行扩展,实现更多的管理操作。 ### 回答3: 顺序表是一种线性表,可以通过数组实现。在游戏战绩管理系统中,我们可以使用模板类来实现通用的数据结构和算法。以下是使用C++语言编写的顺序表的游戏战绩管理系统的示例代码: ```cpp #include <iostream> #include <cstring> using namespace std; template <class T> class SeqList { private: T* data; // 数据数组 int length; // 当前长度 int capacity; // 最大容量 public: SeqList(int cap) { capacity = cap; length = 0; data = new T[capacity]; } ~SeqList() { delete[] data; } void append(T elem) { if (length < capacity) { data[length++] = elem; } else { cout << "顺序表已满,无法插入!" << endl; } } void remove(T elem) { int index = -1; for (int i = 0; i < length; i++) { if (data[i] == elem) { index = i; break; } } if (index == -1) { cout << "未找到要删除的元素!" << endl; } else { for (int i = index; i < length - 1; i++) { data[i] = data[i + 1]; } length--; } } void print() { cout << "游戏战绩管理系统:" << endl; for (int i = 0; i < length; i++) { cout << data[i] << " "; } cout << endl; } }; int main() { SeqList<int> seqList(5); seqList.append(98); seqList.append(100); seqList.append(95); seqList.append(87); seqList.print(); seqList.remove(100); seqList.print(); return 0; } ``` 在这个示例代码中,我们使用模板类SeqList来定义了一个顺序表,可以存储任意类型的数据。通过append()函数可以往顺序表中添加元素,remove()函数可以删除指定的元素,print()函数可以输出当前游戏战绩信息。 在main()函数中,我们创建了一个最大容量为5的顺序表seqList,然后依次添加了四个游戏战绩,并输出。接着我们从顺序表中删除了一个战绩,再次输出。 通过这样一个简单的示例,我们可以利用模板类SeqList来实现游戏战绩管理系统,方便地对战绩进行添加、删除和输出等操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值