用C++实现一个简单的整型数组类
该整型数组类包含以下简要几个功能:
- 默认构造函数(最大容量为10) Array()
- 有参构造函数(自定义最大容量) Array(int capacity)
- 获取数组容量 getN()
- 获取当前元素个数 getCount()
- 判断数组是否为空 isEmpty()
- 修改index位置的元素 set(int index, int x)
- 获取index位置的元素 get(int index)
- 查看数组是否包含元素x contains(int x)
- 获取对应元素e的下标,未找到,返回-1 find(int x)
- 在index位置,插入整数x add(int index, int x)
- 向数组头插入元素 addFirst(int x)
- 向数组尾插入元素 addLast(int x)
- 删除index位置的元素,并返回 remove(int index)
- 删除首元素 removeFirst()
- 删除尾元素 removeLast()
- 从数组中删除指定元素 removeElement(int x)
- 数组动态更容方法 resize(int capacity)
// 自定义整型数组类
class Array {
private:
//定义整型数据data保存数据
int *data;
//定义数组长度
int n;
//定义中实际个数
int count;
public:
//构造方法,定义数组大小
Array(int capacity)
{
data = new int[capacity];
n = capacity;
count = 0;//一开始一个数都没有存所以为0
}
//默认构造方法
Array()
{
// 默认数组长度为10
data = new int[10];
n = 10;
count = 0;
}
// 获取数组容量
int getN()
{
return n;
}
// 获取当前元素个数
int getCount()
{
return count;
}
// 判断数组是否为空
bool isEmpty()
{
return count == 0;
}
// 修改index位置的元素
void set(int index, int x)
{
checkIndex(index);
data[index] = x;
}
// 获取index位置的元素
int get(int index)
{
if (index < 0 || index >= count) return -1;
return data[index];
}
// 查看数组是否包含元素x
bool contains(int x)
{
for (int i = 0; i < count; ++i)
{
if (data[i] == x)
{
return true;
}
}
return false;
}
// 获取对应元素e的下标,未找到,返回-1(第一次出现的元素)
int find(int x)
{
for (int i = 0; i < count; ++i)
{
if (data[i] == x)
{
return i;
}
}
return -1;
}
// 在index位置,插入整数x,时间复杂度O(m+n)
void add(int index, int x)
{
checkIndex(index);
if (count == n)
{
resize(2 * count);
}
for (int i = count - 1; i >= index; --i)
{
data[i + 1] = data[i];
}
data[index] = x;
++count;
}
// 向数组头插入元素
void addFirst(int x)
{
add(0, x);
}
// 向数组尾插入元素
void addLast(int x)
{
add(count, x);
}
// 删除index位置的元素,并返回
int remove(int index)
{
checkIndexForRemove(index);
int ret = data[index];
for (int i = index + 1; i < count; ++i)
{
data[i - 1] = data[i];
}
--count;
data[count] = NULL;
if (count == n / 4 && n / 2 != 0)
{
resize(n / 2);
}
return ret;
}
// 删除首元素
int removeFirst()
{
return remove(0);
}
// 删除尾元素
int removeLast()
{
return remove(count - 1);
}
// 从数组中删除指定元素(第一个出现的元素)
void removeElement(int x)
{
int index = find(x);
if (index != -1)
{
remove(index);
}
}
// 数组扩容方法,时间复杂度O(n)
void resize(int capacity)
{
n = capacity;
int *newData = new int[capacity];
for (int i = 0; i < count; ++i)
{
newData[i] = data[i];
}
int *flag = data;
data = newData;
delete[] flag;
}
// 检测下标异常方法
void checkIndex(int index)
{
if (index < 0 || index > count)
{
throw exception("IndexOutOfBoundsException!");
}
}
// 删除时检查下标异常方法
void checkIndexForRemove(int index)
{
if (index < 0 || index >= count)
{
throw exception("IndexOutOfBoundsException!");
}
}
// 打印数组全部元素
void printAll()
{
for (int i = 0; i < count; ++i) {
cout << data[i] << " ";
}
cout << endl;
}
};
功能检测
检测主函数如下:
int main()
{
// 定义一个容量为5的数组
Array array(5);
// 依次向数组中插入5个整数(头插法)
array.addFirst(1);
array.addFirst(2);
array.addFirst(3);
array.addFirst(4);
array.addFirst(5);
// <1> 查看当前数组容量
cout << "<1> for now the capacity of the array is : " << array.getN() << endl;
// 再依次向数组中插入5个整数(尾插法)
array.addLast(6);
array.addLast(7);
array.addLast(8);
array.addLast(9);
array.addLast(10);
// <2> 查看当前数组容量
cout << "<2> for now the capacity of the array is : " << array.getN() << endl;
// 向数组指定位置插入2个整数
array.add(5, 11);
array.add(5, 12);
// <3> 查看当前数组容量
cout << "<3> for now the capacity of the array is : " << array.getN() << endl;
// 打印当前数组全部元素
array.printAll();
// 查看数组中整数2的下标
if (array.contains(2))
{
cout << "the index of data 2 is : " << array.find(2) << endl;
}
// 删除数组中整数2元素后再次打印数组
array.removeElement(2);
array.printAll();
// 删除数组中一个元素(首删法)
array.removeFirst();
array.removeFirst();
array.removeFirst();
array.removeFirst();
array.removeFirst();
array.removeFirst();
array.removeFirst();
cout << "<4> for now the capacity of the array is : " << array.getN() << endl;
array.removeFirst();
array.removeFirst();
cout << "<5> for now the capacity of the array is : " << array.getN() << endl;
array.removeFirst();
cout << "<6> for now the capacity of the array is : " << array.getN() << endl;
return 0;
}
调试执行结果:
根据主函数测试编写好的Array类,测试了其主要功能以及动态更容的机制,并完成基本功能的调试。
拓展说明
该数组类实现了简单的几个功能,以及动态扩容和动态缩容的过程。默认的动态扩容机制是将数组容量扩大为原先的两倍。而动态缩容机制则是当数组内包含元素数量等于数组容量的四分之一时,缩小数组容量至原来的一半。
此次只是尝试基于元素为整数的数组类编写,等学习熟悉C++模板类的编写后,再尝试进一步编写"真正意义上的"数组类。
未完待续......