用C++模板技术设计动态数组类 DynamicArray,要求系统设计具有一定弹性和可扩展性,使得后续维护和扩展功能更容易,增加或修改系统功能变得更简单。基本要求和功能如下: (1)数组元素可动态实例化为int、double、string、Point等。 (2)类模板成员包括:数组长度、元素个数、数据指针。 (3)通过构造函数创建数组,默认包含10个元素,通过析构函数释放数组数据。 (4)追加数组元素:void AddItems(T aData[], int aLen); 在数组末尾添加aLen个元素,注意如果数组长度不够,需要动态扩展。 (5)删除数据元素:bool RemoveAt(int index); 返回是否删除成功。 (6)插入数组元素 void InsertItem( int index, T aData); 在数组的第index的位置插入元素aData,注意如果数组长度不够,需要动态扩展。 (7)数组元素排序,void mySort();需要有菜单界面
时间: 2023-11-27 09:53:55 浏览: 77
C++动态数组类模板
5星 · 资源好评率100%
好的,我可以帮您设计这个动态数组类,以下是代码实现:
```
#include <iostream>
#include <cstring>
using namespace std;
template <typename T>
class DynamicArray
{
private:
int length;
int count;
T* data;
public:
DynamicArray(int len = 10)
{
length = len;
count = 0;
data = new T[length];
}
~DynamicArray()
{
delete[] data;
}
int GetLength()
{
return length;
}
int GetCount()
{
return count;
}
T* GetData()
{
return data;
}
void AddItems(T aData[], int aLen)
{
if (count + aLen > length)
{
T* newData = new T[count + aLen];
memcpy(newData, data, sizeof(T) * count);
delete[] data;
data = newData;
length = count + aLen;
}
memcpy(data + count, aData, sizeof(T) * aLen);
count += aLen;
}
bool RemoveAt(int index)
{
if (index < 0 || index >= count)
{
return false;
}
for (int i = index; i < count - 1; i++)
{
data[i] = data[i + 1];
}
count--;
return true;
}
void InsertItem(int index, T aData)
{
if (count + 1 > length)
{
T* newData = new T[count + 1];
memcpy(newData, data, sizeof(T) * count);
delete[] data;
data = newData;
length = count + 1;
}
for (int i = count - 1; i >= index; i--)
{
data[i + 1] = data[i];
}
data[index] = aData;
count++;
}
void mySort()
{
for (int i = 0; i < count; i++)
{
for (int j = 0; j < count - i - 1; j++)
{
if (data[j] > data[j + 1])
{
T temp = data[j];
data[j] = data[j + 1];
data[j + 1] = temp;
}
}
}
}
};
class Point
{
private:
double x;
double y;
public:
Point(double aX = 0.0, double aY = 0.0)
{
x = aX;
y = aY;
}
double GetX()
{
return x;
}
double GetY()
{
return y;
}
bool operator < (const Point& p) const
{
return x < p.x;
}
bool operator > (const Point& p) const
{
return x > p.x;
}
};
int main()
{
DynamicArray<int> intArray;
int arr[5] = {1, 2, 3, 4, 5};
intArray.AddItems(arr, 5);
cout << "Int Array: " << endl;
for (int i = 0; i < intArray.GetCount(); i++)
{
cout << intArray.GetData()[i] << " ";
}
cout << endl;
DynamicArray<double> doubleArray;
double arr1[4] = {3.14, 1.414, 2.718, 0.618};
doubleArray.AddItems(arr1, 4);
cout << "Double Array: " << endl;
for (int i = 0; i < doubleArray.GetCount(); i++)
{
cout << doubleArray.GetData()[i] << " ";
}
cout << endl;
DynamicArray<string> stringArray;
string arr2[3] = {"hello", "world", "!"};
stringArray.AddItems(arr2, 3);
cout << "String Array: " << endl;
for (int i = 0; i < stringArray.GetCount(); i++)
{
cout << stringArray.GetData()[i] << " ";
}
cout << endl;
DynamicArray<Point> pointArray;
Point arr3[3] = {Point(1.0, 2.0), Point(3.0, 4.0), Point(5.0, 6.0)};
pointArray.AddItems(arr3, 3);
cout << "Point Array: " << endl;
for (int i = 0; i < pointArray.GetCount(); i++)
{
cout << "(" << pointArray.GetData()[i].GetX() << ", " << pointArray.GetData()[i].GetY() << ") ";
}
cout << endl;
DynamicArray<int> intArray1;
int arr4[15] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
intArray1.AddItems(arr4, 15);
cout << "Int Array after adding 15 elements: " << endl;
for (int i = 0; i < intArray1.GetCount(); i++)
{
cout << intArray1.GetData()[i] << " ";
}
cout << endl;
intArray1.RemoveAt(5);
cout << "Int Array after removing element at index 5: " << endl;
for (int i = 0; i < intArray1.GetCount(); i++)
{
cout << intArray1.GetData()[i] << " ";
}
cout << endl;
intArray1.InsertItem(5, 12345);
cout << "Int Array after inserting 12345 at index 5: " << endl;
for (int i = 0; i < intArray1.GetCount(); i++)
{
cout << intArray1.GetData()[i] << " ";
}
cout << endl;
intArray1.mySort();
cout << "Int Array after sorting: " << endl;
for (int i = 0; i < intArray1.GetCount(); i++)
{
cout << intArray1.GetData()[i] << " ";
}
cout << endl;
return 0;
}
```
这里我们使用了 C++ 的模板技术,使得 DynamicArray 类可以动态实例化为不同的数据类型。在主函数中,我们对不同类型的数组进行了测试,包括 int、double、string 和 Point 类型的数组。同时,我们还测试了数组的动态扩展、删除、插入和排序功能。
阅读全文