#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <string>
using namespace std;
template<typename T>
class Array
{
public:
Array(int capacity) {
data = new T[capacity];
size = 0;
this->capacity = capacity;
}
Array() {
this->capacity = 2;
data = new T[capacity];
size = 0;
}
T& operator[](int i) {
return data[i];
}
int GetSize() {
return size;
}
//可能有问题
int GetCapacity() {
return capacity;
}
bool IsEmpty() {
return size == 0;
}
void add(int index, T e) {
if (size == capacity) {
cout << "array is full,resize?(YorN)" << endl;
char tmp;
cin >> tmp;
if (tmp == 'Y')
{
reSize(this->capacity*2);
}
else
{
throw "array is full";
}
}
if (index<0 || index>size) {
throw "add::index<0 || index>size";
}
for (int i = size - 1; i >= index; i--)
C++使用模板创建动态数组类
最新推荐文章于 2023-03-15 16:43:43 发布