本篇主要是说明数组的封装,数组的具体介绍一搜一大把,这里就不一一说明,这里主要是数组数组相关操作的封装实现,代码不复杂,就不作算法介绍了。
1.数组简介
数组是一个固定长度的存储相同数据类型的数据结构,数组中的元素被存储在一段连续的内存空间中。
1.1.数组的优点:
1、按照索引查询元素速度快;
2、能存储大量数据;
3、按照索引遍历数组方便;
4、数组定义简单,而且访问很方便;
5、可以随机访问其中的元素。
1.2.数组的缺点:
1、根据内容查找元素速度慢;
2、数组的大小一经确定不能改变,不适合动态存储;
3、数组只能存储一种类型的数据;
4、增加、删除元素效率慢;
5、未封装任何方法,所有操作都需要用户自己定义;
6、数组的空间必须是连续的,这就造成数组在内存中分配空间时必须找到一块连续的内存空间。
1.3.数组的定义与使用
在使用一个数组之前,我们需要声明这个数组。声明数组意味着我们需要指定以下内容:
1.数据类型:这个数组将要存储怎样一种类型的数据(不同类型占据的内存空间是不同的.如:字符、整型、浮点型等一些数据类型。
2.名称:用于标识这个数组并与之交互的变量名
3.数量:数组内数据的数量,它指定这个数组最多能存储的数据元素的数量
1.3.1.一维数组
一维数组定义形式如下:
type array[size];
如需要保存10个int型变量,则定义为:
int array[10];
可初始化:
int arrat[10] = {1,2,3,4,5,6,7,8,9,10};
动态申请内存为:
int* array = new int[10];
一维数组遍历如下:
for(int i = 0;i < 10;i++) {
cout << array[i] << ", ";
}
1.3.2.二维数组
二维数组定义形式如下:
type array[row][col];
如需要表示存储int型的3 * 3的矩阵,则定义为:
int matrix[3][3];
可初始化:
int matrix[3][3] = {
{1,2,3},
{4,5,6},
{7,8,9}};
动态申请内存为:
int** matrix = new int*[3];
for(int i = 0;i < 3;i++) {
matrix[i] = new int[3];
}
二维数组遍历如下:
for(int i = 0;i < 3;i++) {
for(int j = 0;j < 3;j++) {
cout << array[i][j] << ", ";
}
}
1.4.数组相关操作
插入、删除、查找、替换、反转、排序、截取、遍历、拼接等。 不使用标准模板库的数组容器,则需要我们自定义相关方法,在此封装一个数组类,封装了以上序列操作,技术不过关,如有错误,请以斧正。
数组插入数据的方法
1.头部插入
2.尾部插入
3.指定位置插入
4.指定区间插入:在区间内插入同一个数据或者一段数据
数组删除数据的方法
1.删除头部
2.删除尾部
3.删除指定位置
4.删除指定数据(只要等于就删除)
5.删除指定位置指定数据(如果这个位置的数据等于要删除的数据就删除)
6.删除指定区间(删除范围内所有数据)
数组查找数据的方法
1.顺序查找指定数据
2.指定位置查找指定数据
3.指定区间查找数据
数组替换数据的方法
1.替换指定位置数据
2.替换指定目标数据
3.替换指定区间数据
数组反转数据的方法
1.整体反转
2.指定区间反转
数组排序数据的方法
1.冒泡排序
2.选择排序
数组截取数据的方法
1.截取区间数据
2.截取指定位置往后数据
数组遍历数据的方法(用于封装之后的遍历)
getData(int index)遍历
重载[]遍历
at(int index)遍历
数组拼接数据的方法(封装之后)
重载+拼接
2.Array类
封装数组数据和操作数据的方法。
2.1.变量表
变量名 | 类型 | 属性 | 说明 |
---|
m_array | Type* | 私有 | 存储数据 | m_len | size_t | 私有 | 表示存储有多少个数据 |
2.2.方法表
方法名 | 返回类型 | 参数 | 属性 | 说明 |
---|
Array() | - | - | 公有 | 缺省构造 | Array() | - | size_t len | 公有 | 传长度构造 | Array() | - | Type* array, size_t len | 公有 | 传数组与长度构造 | Array() | - | const Array& a | 公有 | 拷贝构造 | operator = () | Array& | const Array& a | 公有 | =运算符重载 | ~Array() | - | - | 公有 | 析构函数 | size() const | size_t | - | 公有 | 获取数据个数 | length() const | size_t | - | 公有 | 获取数据个数 | setData() | void | int index, Type value | 公有 | 设置某个位置的值 | empty() const | bool | - | 公有 | 判断是否为空 | push_front() | bool | Type value | 公有 | 头部插入数据 | push_back() | bool | Type value | 公有 | 尾部插入数据 | insert() | bool | int index, Type value | 公有 | 指定位置插入数据 | insert() | bool | int begin, int end, Type value | 公有 | 指定区间插入数据 | pop_front() | int | - | 公有 | 删除头部数据 | pop_back() | int | - | 公有 | 删除尾部数据 | eraseIndex() | bool | int index | 公有 | 删除指定位置数据 | eraseValue() | bool | Type value | 公有 | 删除指定数据 | eraseIndexValue() | bool | int index, Type value | 公有 | 删除指定位置指定数据 | eraseSection() | bool | int begin, int end | 公有 | 删除指定区间数据 | selectIndex() | int | int val | 公有 | 顺序查找数据 | selectIndexValue() | bool | int index, Type val | 公有 | 查找指定位置指定数据 | selectSection() | bool | int begin, int end, Type value | 公有 | 指定区间查找数据 | replaceIndex() | bool | int index, Type value | 公有 | 替换指定位置数据 | replaceValue() | bool | Type target, Type value | 公有 | 替换目标数据 | replaceSection() | bool | int begin, int end, Type value | 公有 | 替换区间数据 | reverse() | Array | - | 公有 | 整体反转 | reverse() | Array | int begin, int end | 公有 | 局部反转 | bubbleSort() | Array | - | 公有 | 冒泡排序 | selectionSort() | Array | - | 公有 | 选择排序 | subArray() | Array | int begin, int end | 截取区间数据 | | subArray() | Array | int begin | 公有 | 截取指定位置往后数据 | getData() | Type | int index | 公有 | 返回指定位置数据 | operator [] () | Type& | int index | 公有 | 返回指定位置数据 | at() | Type | int index | 公有 | 返回指定位置数据 | operator + () | Array | Array a, Array b | 友元函数 | 数组拼接 | begin() | Iterator | - | 公有 | 正向迭代器起始位置 | end() | Iterator | - | 公有 | 正向迭代器结束位置 | cbegin() | ConstIterator | - | 公有 | 正向常量迭代器起始位置 | cend() | ConstIterator | - | 公有 | 正向常量迭代器结束位置 | rbegin() | ReverseIterator | - | 公有 | 反向迭代器起始位置 | rend() | ReverseIterator | - | 公有 | 反向迭代器结束位置 | crbegin() | ConstReverseIterator | - | 公有 | 反向常量迭代器起始位置 | crend() | ConstReverseIterator | - | 公有 | 反向常量迭代器结束位置 |
2.3.迭代器
简单的实现四个迭代器,没有很多方法。
2.3.1.变量
变量名 | 类型 | 属性 | 说明 |
---|
m_arr | Array* | 私有 | 使用迭代器的当前数组 | m_position | int | 私有 | 迭代器的位置 |
2.3.2.方法
方法名 | 返回类型 | 参数 | 属性 | 说明 |
---|
iterator() | - | - | 公有 | 缺省构造 | iterator() | - | Array* arr, int position | 公有 | 传值构造 | operator == () | bool | const iterator& itb | 公有 | ==运算符重载 | operator != () | bool | const iterator& itb | 公有 | !=运算符重载 | operator + () | iterator | int off | 公有 | +运算符重载,支持随机访问 | operator - () | iterator | int off | 公有 | -运算符重载,支持随机访问 | operator ++ () | iterator | - | 公有 | 前置++ | operator ++ () | iterator | int | 公有 | 后置++ | operator – () | iterator | - | 公有 | 前置– | operator – () | iterator | int | 公有 | 后置– | operator * () | Type& | - | 公有 | *运算符重载,常量迭代器返回值为Type |
3.测试
测试封装的数据和方法是否有问题。
3.1.构造函数测试
测试代码:
std::cout << "\n缺省构造:" << std::endl;
Array<int> arr;
std::cout << "\n传长度构造:" << std::endl;
Array<int> arr2(8);
std::cout << "\n传数组,长度构造:" << std::endl;
int array[7]{};
Array<int> arr3(array, 7);
std::cout << "\n拷贝构造:" << std::endl;
Array<int> arr4(arr2);
输出结果:
缺省构造:
传长度构造:
传数组,长度构造:
拷贝构造:
3.2.插入测试
测试代码:
std::cout << "\n传长度构造:" << std::endl;
Array<int> arr(8);
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n头部插入:" << std::endl;
arr.push_front(9);
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n尾部插入:" << std::endl;
arr.push_back(7);
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n指定位置插入:" << std::endl;
arr.insert(3, 16);
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n指定区间插入:" << std::endl;
arr.insert(4, 7, 16);
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
输出结果:
传长度构造:
0, 0, 0, 0, 0, 0, 0, 0,
头部插入:
9, 0, 0, 0, 0, 0, 0, 0, 0,
尾部插入:
9, 0, 0, 0, 0, 0, 0, 0, 0, 7,
指定位置插入:
9, 0, 0, 16, 0, 0, 0, 0, 0, 0, 7,
指定区间插入:
9, 0, 0, 16, 16, 16, 16, 16, 0, 0, 0, 0, 0, 0, 7,
3.3.删除测试
测试代码:
std::cout << "\n传长度构造:" << std::endl;
Array<int> arr(12);
int num = 1;
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
arr[i] = num++;
}
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n头部删除:" << std::endl;
arr.pop_front();
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n尾部删除:" << std::endl;
arr.pop_back();
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n指定位置删除:" << std::endl;
arr.eraseIndex(3);
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n指定位置指定数据删除:" << std::endl;
arr.eraseIndexValue(3, 8);
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
arr.eraseIndexValue(3, 6);
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n指定数据删除:" << std::endl;
std::cout << "\n在3---7插入19:" << std::endl;
arr.insert(3, 7, 19);
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n指定数据删除:" << std::endl;
arr.eraseValue(19);
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n指定区间删除:" << std::endl;
arr.eraseSection(3, 7);
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
输出结果:
传长度构造:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
头部删除:
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
尾部删除:
2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
指定位置删除:
2, 3, 4, 6, 7, 8, 9, 10, 11,
指定位置指定数据删除:
2, 3, 4, 6, 7, 8, 9, 10, 11,
2, 3, 4, 7, 8, 9, 10, 11,
指定数据删除:
在3---7插入19:
2, 3, 4, 19, 19, 19, 19, 19, 7, 8, 9, 10, 11,
指定数据删除:
2, 3, 4, 7, 8, 9, 10, 11,
指定区间删除:
2, 3, 4,
3.4.查找测试
测试代码:
std::cout << "\n传长度构造:" << std::endl;
Array<int> arr(12);
int num = 1;
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
arr[i] = num++;
}
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n查找指定数据:" << std::endl;
int res = arr.selectIndex(9);
if (res != -1) {
std::cout << "在" << res << "处" << std::endl;
}
else {
std::cout << "找不到!" << std::endl;
}
std::cout << "\n查找指定位置指定数据:" << std::endl;
if (arr.selectIndexValue(3, 9)) {
std::cout << "在3处是9" << std::endl;
}
else {
std::cout << "在3处不是9" << std::endl;
}
std::cout << "\n查找指定指定区间数据:" << std::endl;
if (arr.selectSection(3, 9, 5)) {
std::cout << "5在3-------9之间" << std::endl;
}
else {
std::cout << "5不在3-------9之间" << std::endl;
}
输出结果:
传长度构造:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
查找指定数据:
在8处
查找指定位置指定数据:
在3处不是9
查找指定指定区间数据:
5在3-------9之间
3.5.替换测试
测试代码:
std::cout << "\n传长度构造:" << std::endl;
Array<int> arr(12);
int num = 1;
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
arr[i] = num++;
}
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n替换指定数据:" << std::endl;
arr.replaceIndex(1, 44);
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n替换目标数据:" << std::endl;
std::cout << "\n在1---5插入22:" << std::endl;
arr.insert(1, 5, 22);
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n将22替换为44:" << std::endl;
arr.replaceValue(22, 44);
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n替换区间数据:" << std::endl;
arr.replaceSection(1, 12, 33);
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
输出结果:
传长度构造:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
替换指定数据:
1, 44, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
替换目标数据:
在1---5插入22:
1, 22, 22, 22, 22, 22, 44, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
将22替换为44:
1, 44, 44, 44, 44, 44, 44, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
替换区间数据:
1, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 9, 10, 11, 12,
3.6.反转测试
测试代码:
std::cout << "\n传长度构造:" << std::endl;
Array<int> arr(12);
int num = 1;
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
arr[i] = num++;
}
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n整体反转:" << std::endl;
Array<int> arr2 = arr.reverse();
for (int i = 0; i < static_cast<int>(arr2.size()); i++) {
std::cout << arr2[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n区间反转:" << std::endl;
Array<int> arr3 = arr.reverse(4, 7);
for (int i = 0; i < static_cast<int>(arr3.size()); i++) {
std::cout << arr3[i] << ", ";
}
std::cout << std::endl;
输出结果:
传长度构造:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
整体反转:
12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1,
区间反转:
1, 2, 3, 4, 8, 7, 6, 5, 9, 10, 11, 12,
3.7.排序测试
测试代码:
std::cout << "\n传长度构造:" << std::endl;
Array<int> arr(12);
int num = 1;
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
arr[i] = num++ % 5;
}
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n冒泡排序:" << std::endl;
Array<int> arr2 = arr.bubbleSort();
for (int i = 0; i < static_cast<int>(arr2.size()); i++) {
std::cout << arr2[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n选择排序:" << std::endl;
Array<int> arr3 = arr.selectionSort();
for (int i = 0; i < static_cast<int>(arr3.size()); i++) {
std::cout << arr3[i] << ", ";
}
std::cout << std::endl;
输出结果:
传长度构造:
1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2,
冒泡排序:
0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 4, 4,
选择排序:
0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 4, 4,
3.8.截取测试
测试代码:
std::cout << "\n传长度构造:" << std::endl;
Array<int> arr(12);
int num = 1;
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
arr[i] = num++;
}
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n截取区间数据:" << std::endl;
Array<int> arr2 = arr.subArray(3, 9);
for (int i = 0; i < static_cast<int>(arr2.size()); i++) {
std::cout << arr2[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n截取指定位置往后数据:" << std::endl;
Array<int> arr3 = arr.subArray(3);
for (int i = 0; i < static_cast<int>(arr3.size()); i++) {
std::cout << arr3[i] << ", ";
}
std::cout << std::endl;
输出结果:
传长度构造:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
截取区间数据:
4, 5, 6, 7, 8, 9, 10,
截取指定位置往后数据:
4, 5, 6, 7, 8, 9, 10, 11, 12,
3.9.遍历测试
测试代码:
std::cout << "\n传长度构造:" << std::endl;
Array<int> arr(12);
int num = 1;
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
arr[i] = num++;
}
std::cout << "\n重载[]遍历:" << std::endl;
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
std::cout << "\nat函数遍历:" << std::endl;
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr.at(i) << ", ";
}
std::cout << std::endl;
std::cout << "\ngetData函数遍历:" << std::endl;
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr.getData(i) << ", ";
}
std::cout << std::endl;
输出结果:
传长度构造:
重载[]遍历:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
at函数遍历:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
getData函数遍历:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
3.10.连接测试
测试代码:
std::cout << "\n传长度构造:" << std::endl;
Array<int> arr(12);
int num = 1;
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
arr[i] = num++;
}
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n传长度构造:" << std::endl;
Array<int> arr2(10);
num = 1;
for (int i = 0; i < static_cast<int>(arr2.size()); i++) {
arr2[i] = num++;
}
for (int i = 0; i < static_cast<int>(arr2.size()); i++) {
std::cout << arr2[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n连接:" << std::endl;
Array<int> arr3 = arr + arr2;
for (int i = 0; i < static_cast<int>(arr3.size()); i++) {
std::cout << arr3[i] << ", ";
}
std::cout << std::endl;
输出结果:
传长度构造:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
传长度构造:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
连接:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
3.11.迭代器测试
测试代码:
std::cout << "\n传长度构造:" << std::endl;
Array<int> arr(12);
int num = 1;
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
arr[i] = num++;
}
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n正向迭代器遍历:" << std::endl;
for (Array<int>::Iterator it = arr.begin(); it != arr.end(); it++) {
std::cout << *it << ", ";
}
std::cout << std::endl;
std::cout << "\n正向常量迭代器遍历:" << std::endl;
for (Array<int>::ConstIterator it = arr.cbegin(); it != arr.cend(); it++) {
std::cout << *it << ", ";
}
std::cout << std::endl;
std::cout << "\n反向迭代器遍历:" << std::endl;
for (Array<int>::ReverseIterator it = arr.rbegin(); it != arr.rend(); it++) {
std::cout << *it << ", ";
}
std::cout << std::endl;
std::cout << "\n反向常量迭代器遍历:" << std::endl;
for (Array<int>::ConstReverseIterator it = arr.crbegin(); it != arr.crend(); it++) {
std::cout << *it << ", ";
}
std::cout << std::endl;
std::cout << "\n正向迭代器修改值:" << std::endl;
for (Array<int>::Iterator it = arr.begin(); it != arr.end(); it++) {
*it = 7;
}
for (Array<int>::Iterator it = arr.begin(); it != arr.end(); it++) {
std::cout << *it << ", ";
}
std::cout << std::endl;
std::cout << "\n反向迭代器修改值:" << std::endl;
for (Array<int>::ReverseIterator it = arr.rbegin(); it != arr.rend(); it++) {
*it = num++;
}
for (Array<int>::ReverseIterator it = arr.rbegin(); it != arr.rend(); it++) {
std::cout << *it << ", ";
}
std::cout << std::endl;
for (Array<int>::Iterator it = arr.begin(); it != arr.end(); it++) {
std::cout << *it << ", ";
}
std::cout << std::endl;
std::cout << "\n迭代器随机访问测试:" << std::endl;
auto it = arr.begin();
std::cout << *it << std::endl;
it = it + 5;
std::cout << *it << std::endl;
输出结果:
传长度构造:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
正向迭代器遍历:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
正向常量迭代器遍历:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
反向迭代器遍历:
12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1,
反向常量迭代器遍历:
12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1,
正向迭代器修改值:
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
反向迭代器修改值:
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13,
迭代器随机访问测试:
24
19
4.实现代码
#pragma once
#ifndef _ARRAY_H_
#define _ARRAY_H_
#include <iostream>
#include <cassert>
template <typename Type>
class Array
{
public:
Array()
{
m_array = nullptr;
m_len = 0;
}
Array(size_t len)
{
m_array = new Type[len];
for (int i = 0; i < len; i++) {
m_array[i] = Type();
}
m_len = len;
}
Array(Type* array, size_t len)
{
if (array == nullptr) {
m_array = nullptr;
m_len = 0;
}
else {
m_array = new Type[len];
for (int i = 0; i < len; i++) {
m_array[i] = array[i];
}
m_len = len;
}
}
Array(const Array& a)
{
m_len = a.m_len;
if (m_len == 0) {
m_array = nullptr;
}
else {
m_array = new Type[m_len];
for (int i = 0; i < m_len; i++) {
m_array[i] = a.m_array[i];
}
}
}
Array& operator = (const Array& a)
{
if (m_array != nullptr) {
delete[]m_array;
}
m_len = a.m_len;
if (m_len == 0) {
m_array = nullptr;
}
else {
m_array = new Type[m_len];
for (int i = 0; i < m_len; i++) {
m_array[i] = a.m_array[i];
}
}
return *this;
}
~Array()
{
if (m_array != nullptr) {
delete[]m_array;
m_array = nullptr;
}
}
size_t size() const
{
return m_len;
}
size_t length() const
{
return m_len;
}
void setData(int index, Type value)
{
assert(index >= 0 && index < m_len);
m_array[index] = value;
}
bool empty() const
{
return m_array == nullptr;
}
bool push_front(Type value)
{
if (m_array == nullptr) {
m_array = new int(value);
if (m_array == nullptr) {
return false;
}
m_len = 1;
}
else {
m_len++;
Type* newArray = new Type[m_len];
if (newArray == nullptr) {
return false;
}
for (int i = 1; i < m_len; i++) {
newArray[i] = m_array[i - 1];
}
newArray[0] = value;
delete[]m_array;
m_array = newArray;
}
return true;
}
bool push_back(Type value)
{
if (m_array == nullptr) {
m_array = new int(value);
if (m_array == nullptr) {
return false;
}
m_len = 1;
}
else {
m_len++;
Type* newArray = new Type[m_len];
if (newArray == nullptr) {
return false;
}
for (int i = 0; i < m_len - 1; i++) {
newArray[i] = m_array[i];
}
newArray[m_len - 1] = value;
delete[]m_array;
m_array = newArray;
}
return true;
}
bool insert(int index, Type value)
{
if (index == 0) {
return push_front(value);
}
else if (index == m_len) {
return push_back(value);
}
else if (index > m_len) {
return false;
}
m_len++;
Type* newArray = new Type[m_len];
if (newArray == nullptr) {
return false;
}
for (int i = 0; i < m_len - 1; i++) {
if (i < index) {
newArray[i] = m_array[i];
}
else {
newArray[i + 1] = m_array[i];
}
}
newArray[index] = value;
delete[]m_array;
m_array = newArray;
return true;
}
bool insert(int begin, int end, Type value)
{
if (begin == end) {
return insert(begin, value);
}
else if (begin > end || end >= m_len) {
false;
}
int count = static_cast<unsigned long long>(end) - begin + 1;
m_len += count;
Type* newArray = new Type[m_len];
if (newArray == nullptr) {
return false;
}
for (int i = 0; i < m_len; i++) {
if (i < begin) {
newArray[i] = m_array[i];
}
else if (i <= end) {
newArray[i] = value;
}
else {
newArray[i] = m_array[i - count];
}
}
delete[]m_array;
m_array = newArray;
return true;
}
int pop_front()
{
assert(m_array != nullptr);
int value = *m_array;
if (m_len == 1) {
delete[]m_array;
m_array = nullptr;
m_len = 0;
}
else {
m_len--;
Type* newArray = new Type[m_len];
for (int i = 0; i < m_len; i++) {
newArray[i] = m_array[i + 1];
}
delete[]m_array;
m_array = newArray;
}
return value;
}
int pop_back()
{
assert(m_array != nullptr);
int value = m_array[m_len - 1];
if (m_len == 1) {
delete[]m_array;
m_array = nullptr;
m_len = 0;
}
else {
m_len--;
Type* newArray = new Type[m_len];
for (int i = 0; i < m_len; i++) {
newArray[i] = m_array[i];
}
delete[]m_array;
m_array = newArray;
}
return value;
}
bool eraseIndex(int index)
{
if (index < 0 || index >= m_len) {
return false;
}
if (m_len == 1) {
delete[]m_array;
m_array = nullptr;
m_len = 0;
}
else {
m_len--;
Type* newArray = new Type[m_len];
for (int i = 0; i < m_len; i++) {
if (i < index) {
newArray[i] = m_array[i];
}
else {
newArray[i] = m_array[i + 1];
}
}
delete[]m_array;
m_array = newArray;
}
return true;
}
bool eraseValue(Type value)
{
int i = 0;
while (i < m_len) {
if (m_array[i] == value) {
eraseIndex(i);
}
else {
i++;
}
}
return true;
}
bool eraseIndexValue(int index, Type value)
{
if (index < 0 || index >= m_len) {
return false;
}
if (m_array[index] == value) {
return eraseIndex(index);
}
return false;
}
bool eraseSection(int begin, int end)
{
if (begin < 0 || begin > end || end >= m_len) {
return false;
}
int i = begin;
while (i <= end) {
eraseIndex(begin);
i++;
}
return true;
}
int selectIndex(int val)
{
for (int i = 0; i < m_len; i++) {
if (m_array[i] == val) {
return i;
}
}
return -1;
}
bool selectIndexValue(int index, Type val)
{
if (index < 0 || index >= m_len) {
return false;
}
return m_array[index] == val;
}
bool selectSection(int begin, int end, Type value)
{
if (begin < 0 || begin > end || end >= m_len) {
return false;
}
for (int i = begin; i <= end; i++) {
if (m_array[i] == value) {
return true;
}
}
return false;
}
bool replaceIndex(int index, Type value)
{
if (index < 0 || index >= m_len) {
return false;
}
m_array[index] = value;
return true;
}
bool replaceValue(Type target, Type value)
{
for (int i = 0; i < m_len; i++) {
if (m_array[i] == target) {
m_array[i] = value;
}
}
return true;
}
bool replaceSection(int begin, int end, Type value)
{
if (begin < 0 || begin > end || end >= m_len) {
return false;
}
for (int i = begin; i <= end; i++) {
m_array[i] = value;
}
return true;
}
Array<Type> reverse()
{
Array<Type> arr(m_len );
for (int i = 0; i < static_cast<int>(m_len); i++) {
arr[i] = this->operator[](static_cast<int>(m_len) - i - 1);
}
return arr;
}
Array<Type> reverse(int begin, int end)
{
if (begin < 0 || begin > end || end >= m_len) {
return Array<Type>();
}
Array<Type> arr(*this);
for (int i = begin, j = end; i < j; i++, j--) {
std::swap(arr[i], arr[j]);
}
return arr;
}
Array<Type> bubbleSort()
{
Array<Type> arr(*this);
for (int i = 0; i < m_len; i++) {
for (int j = 0; j < m_len - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
std::swap(arr[j], arr[j + 1]);
}
}
}
return arr;
}
Array<Type> selectionSort()
{
Array<Type> arr(*this);
for (int i = 0; i < m_len - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < m_len; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
if (minIndex != i) {
std::swap(arr[i], arr[minIndex]);
}
}
return arr;
}
Array<Type> subArray(int begin, int end)
{
if (begin < 0 || begin > end || end >= m_len) {
return Array<Type>();
}
int len = end - begin + 1;
Type* subarr = new Type[len];
for (int i = begin; i <= end; i++) {
subarr[i - begin] = m_array[i];
}
return Array<Type>(subarr, len);
}
Array<Type> subArray(int begin)
{
if (begin < 0 || begin >= m_len) {
return Array<Type>();
}
int len = static_cast<int>(m_len) - begin;
Type* subarr = new Type[len];
for (int i = begin; i < m_len; i++) {
subarr[i - begin] = m_array[i];
}
return Array<Type>(subarr, len);
}
Type getData(int index)
{
assert(index >= 0 && index < m_len);
return m_array[index];
}
Type& operator [] (int index)
{
assert(index >= 0 && index < m_len);
return m_array[index];
}
Type at(int index)
{
assert(index >= 0 && index < m_len);
return m_array[index];
}
friend Array operator + (Array a, Array b)
{
size_t alen = a.size();
size_t blen = b.size();
if (alen == 0 && blen == 0) {
return Array();
}
size_t len = alen + blen;
Array c(len);
int i = 0;
while (i < len) {
if (i < alen) {
c[i] = a[i];
}
else {
c[i] = b[i - static_cast<int>(alen)];
}
i++;
}
return c;
}
private:
Type* m_array;
size_t m_len;
public:
class IteratorBase {
public:
IteratorBase()
{
m_arr = nullptr;
m_position = 0;
}
IteratorBase(Array<Type>* arr, int position)
{
m_arr = arr;
m_position = position;
}
protected:
Array<Type>* m_arr;
int m_position;
};
class Iterator : public IteratorBase
{
using MyBase = IteratorBase;
public:
Iterator()
: IteratorBase()
{
}
Iterator(Array<Type>* arr, int position)
: IteratorBase(arr, position)
{
}
bool operator == (const Iterator& itb)
{
return MyBase::m_position == itb.MyBase::m_position;
}
bool operator != (const Iterator& itb)
{
return MyBase::m_position != itb.MyBase::m_position;
}
Iterator operator + (int off)
{
assert(MyBase::m_position + off <= MyBase::m_arr->m_len);
Iterator it = *this;
it.MyBase::m_position += off;
return it;
}
Iterator operator - (int off)
{
assert(MyBase::m_position - off >= 0);
Iterator it = *this;
it.MyBase::m_position -= off;
return it;
}
Iterator operator ++ ()
{
assert(MyBase::m_position + 1 <= MyBase::m_arr->m_len);
MyBase::m_position++;
return *this;
}
Iterator operator ++ (int)
{
assert(MyBase::m_position + 1 <= MyBase::m_arr->m_len);
Iterator& it = *this;
MyBase::m_position++;
return it;
}
Iterator operator -- ()
{
assert(MyBase::m_position - 1 >= 0);
MyBase::m_position--;
return *this;
}
Iterator operator -- (int)
{
assert(MyBase::m_position - 1 >= 0);
Iterator& it = *this;
MyBase::m_position--;
return it;
}
Type& operator * ()
{
return MyBase::m_arr->operator[](MyBase::m_position);
}
};
class ConstIterator : public IteratorBase
{
using MyBase = IteratorBase;
public:
ConstIterator()
: IteratorBase()
{
}
ConstIterator(Array<Type>* arr, int position)
: IteratorBase(arr, position)
{
}
bool operator == (const ConstIterator& itb)
{
return MyBase::m_position == itb.MyBase::m_position;
}
bool operator != (const ConstIterator& itb)
{
return MyBase::m_position != itb.MyBase::m_position;
}
ConstIterator operator + (int off)
{
assert(MyBase::m_position + off <= MyBase::m_arr->m_len);
ConstIterator it = *this;
it.MyBase::m_position += off;
return it;
}
ConstIterator operator - (int off)
{
assert(MyBase::m_position - off >= 0);
ConstIterator it = *this;
it.MyBase::m_position -= off;
return it;
}
ConstIterator operator ++ ()
{
assert(MyBase::m_position + 1 <= MyBase::m_arr->m_len);
MyBase::m_position++;
return *this;
}
ConstIterator operator ++ (int)
{
assert(MyBase::m_position + 1 <= MyBase::m_arr->m_len);
ConstIterator& it = *this;
MyBase::m_position++;
return it;
}
ConstIterator operator -- ()
{
assert(MyBase::m_position - 1 >= 0);
MyBase::m_position--;
return *this;
}
ConstIterator operator -- (int)
{
assert(MyBase::m_position - 1 >= 0);
ConstIterator& it = *this;
MyBase::m_position--;
return it;
}
Type operator * ()
{
return MyBase::m_arr->operator[](MyBase::m_position);
}
};
class ReverseIterator : public IteratorBase
{
using MyBase = IteratorBase;
public:
ReverseIterator()
: IteratorBase()
{
}
ReverseIterator(Array<Type>* arr, int position)
: IteratorBase(arr, position)
{
}
bool operator == (const ReverseIterator& itb)
{
return MyBase::m_position == itb.MyBase::m_position;
}
bool operator != (const ReverseIterator& itb)
{
return MyBase::m_position != itb.MyBase::m_position;
}
ReverseIterator operator + (int off)
{
assert(MyBase::m_position + off <= MyBase::m_arr->m_len);
ReverseIterator it = *this;
it.MyBase::m_position += off;
return it;
}
ReverseIterator operator - (int off)
{
assert(MyBase::m_position - off >= 0);
ReverseIterator it = *this;
it.MyBase::m_position -= off;
return *this;
}
ReverseIterator operator ++ ()
{
assert(MyBase::m_position + 1 <= MyBase::m_arr->m_len);
MyBase::m_position++;
return *this;
}
ReverseIterator operator ++ (int)
{
assert(MyBase::m_position + 1 <= MyBase::m_arr->m_len);
ReverseIterator& it = *this;
MyBase::m_position++;
return it;
}
ReverseIterator operator -- ()
{
assert(MyBase::m_position - 1 >= 0);
MyBase::m_position--;
return *this;
}
ReverseIterator operator -- (int)
{
assert(MyBase::m_position - 1 >= 0);
ReverseIterator& it = *this;
MyBase::m_position--;
return it;
}
Type& operator * ()
{
return MyBase::m_arr->operator[](static_cast<int>(MyBase::m_arr->m_len) - MyBase::m_position - 1);
}
};
class ConstReverseIterator : public IteratorBase
{
using MyBase = IteratorBase;
public:
ConstReverseIterator()
: MyBase()
{
}
ConstReverseIterator(Array<Type>* arr, int position)
: MyBase(arr, position)
{
}
bool operator == (const ConstReverseIterator& itb)
{
return MyBase::m_position == itb.MyBase::m_position;
}
bool operator != (const ConstReverseIterator& itb)
{
return MyBase::m_position != itb.MyBase::m_position;
}
ConstReverseIterator operator + (int off)
{
assert(MyBase::m_position + off <= MyBase::m_arr->m_len);
ConstReverseIterator it = *this;
it.MyBase::m_position += off;
return it;
}
ConstReverseIterator operator - (int off)
{
assert(MyBase::m_position - off >= 0);
ConstReverseIterator it = *this;
it.MyBase::m_position -= off;
return *this;
}
ConstReverseIterator operator ++ ()
{
assert(MyBase::m_position + 1 <= MyBase::m_arr->m_len);
MyBase::m_position++;
return *this;
}
ConstReverseIterator operator ++ (int)
{
assert(MyBase::m_position + 1 <= MyBase::m_arr->m_len);
ConstReverseIterator& it = *this;
MyBase::m_position++;
return it;
}
ConstReverseIterator operator -- ()
{
assert(MyBase::m_position - 1 >= 0);
MyBase::m_position--;
return *this;
}
ConstReverseIterator operator -- (int)
{
assert(MyBase::m_position - 1 >= 0);
ConstReverseIterator& it = *this;
MyBase::m_position--;
return it;
}
Type operator * ()
{
return MyBase::m_arr->operator[](static_cast<int>(MyBase::m_arr->m_len) - MyBase::m_position - 1);
}
};
Iterator begin()
{
return Iterator(this, 0);
}
Iterator end()
{
return Iterator(this, static_cast<int>(m_len));
}
ConstIterator cbegin()
{
return ConstIterator(this, 0);
}
ConstIterator cend()
{
return ConstIterator(this, static_cast<int>(m_len));
}
ReverseIterator rbegin()
{
return ReverseIterator(this, 0);
}
ReverseIterator rend()
{
return ReverseIterator(this, static_cast<int>(m_len));
}
ConstReverseIterator crbegin()
{
return ConstReverseIterator(this, 0);
}
ConstReverseIterator crend()
{
return ConstReverseIterator(this, static_cast<int>(m_len));
}
};
#endif
5.测试代码
#include "Array.h"
void constructorTest();
void insertTest();
void eraseTest();
void selectTest();
void replaceTest();
void reverseTest();
void sortTest();
void subTest();
void ergodicTest();
void connectTest();
void iteratorTest();
int main()
{
constructorTest();
insertTest();
eraseTest();
selectTest();
replaceTest();
reverseTest();
sortTest();
subTest();
ergodicTest();
connectTest();
iteratorTest();
return 0;
}
void constructorTest()
{
std::cout << "\n缺省构造:" << std::endl;
Array<int> arr;
std::cout << "\n传长度构造:" << std::endl;
Array<int> arr2(8);
std::cout << "\n传数组,长度构造:" << std::endl;
int array[7]{};
Array<int> arr3(array, 7);
std::cout << "\n拷贝构造:" << std::endl;
Array<int> arr4(arr2);
}
void insertTest()
{
std::cout << "\n传长度构造:" << std::endl;
Array<int> arr(8);
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n头部插入:" << std::endl;
arr.push_front(9);
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n尾部插入:" << std::endl;
arr.push_back(7);
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n指定位置插入:" << std::endl;
arr.insert(3, 16);
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n指定区间插入:" << std::endl;
arr.insert(4, 7, 16);
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
}
void eraseTest()
{
std::cout << "\n传长度构造:" << std::endl;
Array<int> arr(12);
int num = 1;
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
arr[i] = num++;
}
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n头部删除:" << std::endl;
arr.pop_front();
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n尾部删除:" << std::endl;
arr.pop_back();
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n指定位置删除:" << std::endl;
arr.eraseIndex(3);
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n指定位置指定数据删除:" << std::endl;
arr.eraseIndexValue(3, 8);
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
arr.eraseIndexValue(3, 6);
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n指定数据删除:" << std::endl;
std::cout << "\n在3---7插入19:" << std::endl;
arr.insert(3, 7, 19);
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n指定数据删除:" << std::endl;
arr.eraseValue(19);
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n指定区间删除:" << std::endl;
arr.eraseSection(3, 7);
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
}
void selectTest()
{
std::cout << "\n传长度构造:" << std::endl;
Array<int> arr(12);
int num = 1;
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
arr[i] = num++;
}
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n查找指定数据:" << std::endl;
int res = arr.selectIndex(9);
if (res != -1) {
std::cout << "在" << res << "处" << std::endl;
}
else {
std::cout << "找不到!" << std::endl;
}
std::cout << "\n查找指定位置指定数据:" << std::endl;
if (arr.selectIndexValue(3, 9)) {
std::cout << "在3处是9" << std::endl;
}
else {
std::cout << "在3处不是9" << std::endl;
}
std::cout << "\n查找指定指定区间数据:" << std::endl;
if (arr.selectSection(3, 9, 5)) {
std::cout << "5在3-------9之间" << std::endl;
}
else {
std::cout << "5不在3-------9之间" << std::endl;
}
}
void replaceTest()
{
std::cout << "\n传长度构造:" << std::endl;
Array<int> arr(12);
int num = 1;
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
arr[i] = num++;
}
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n替换指定数据:" << std::endl;
arr.replaceIndex(1, 44);
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n替换目标数据:" << std::endl;
std::cout << "\n在1---5插入22:" << std::endl;
arr.insert(1, 5, 22);
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n将22替换为44:" << std::endl;
arr.replaceValue(22, 44);
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n替换区间数据:" << std::endl;
arr.replaceSection(1, 12, 33);
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
}
void reverseTest()
{
std::cout << "\n传长度构造:" << std::endl;
Array<int> arr(12);
int num = 1;
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
arr[i] = num++;
}
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n整体反转:" << std::endl;
Array<int> arr2 = arr.reverse();
for (int i = 0; i < static_cast<int>(arr2.size()); i++) {
std::cout << arr2[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n区间反转:" << std::endl;
Array<int> arr3 = arr.reverse(4, 7);
for (int i = 0; i < static_cast<int>(arr3.size()); i++) {
std::cout << arr3[i] << ", ";
}
std::cout << std::endl;
}
void sortTest()
{
std::cout << "\n传长度构造:" << std::endl;
Array<int> arr(12);
int num = 1;
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
arr[i] = num++ % 5;
}
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n冒泡排序:" << std::endl;
Array<int> arr2 = arr.bubbleSort();
for (int i = 0; i < static_cast<int>(arr2.size()); i++) {
std::cout << arr2[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n选择排序:" << std::endl;
Array<int> arr3 = arr.selectionSort();
for (int i = 0; i < static_cast<int>(arr3.size()); i++) {
std::cout << arr3[i] << ", ";
}
std::cout << std::endl;
}
void subTest()
{
std::cout << "\n传长度构造:" << std::endl;
Array<int> arr(12);
int num = 1;
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
arr[i] = num++;
}
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n截取区间数据:" << std::endl;
Array<int> arr2 = arr.subArray(3, 9);
for (int i = 0; i < static_cast<int>(arr2.size()); i++) {
std::cout << arr2[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n截取指定位置往后数据:" << std::endl;
Array<int> arr3 = arr.subArray(3);
for (int i = 0; i < static_cast<int>(arr3.size()); i++) {
std::cout << arr3[i] << ", ";
}
std::cout << std::endl;
}
void ergodicTest()
{
std::cout << "\n传长度构造:" << std::endl;
Array<int> arr(12);
int num = 1;
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
arr[i] = num++;
}
std::cout << "\n重载[]遍历:" << std::endl;
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
std::cout << "\nat函数遍历:" << std::endl;
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr.at(i) << ", ";
}
std::cout << std::endl;
std::cout << "\ngetData函数遍历:" << std::endl;
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr.getData(i) << ", ";
}
std::cout << std::endl;
}
void connectTest()
{
std::cout << "\n传长度构造:" << std::endl;
Array<int> arr(12);
int num = 1;
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
arr[i] = num++;
}
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n传长度构造:" << std::endl;
Array<int> arr2(10);
num = 1;
for (int i = 0; i < static_cast<int>(arr2.size()); i++) {
arr2[i] = num++;
}
for (int i = 0; i < static_cast<int>(arr2.size()); i++) {
std::cout << arr2[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n连接:" << std::endl;
Array<int> arr3 = arr + arr2;
for (int i = 0; i < static_cast<int>(arr3.size()); i++) {
std::cout << arr3[i] << ", ";
}
std::cout << std::endl;
}
void iteratorTest()
{
std::cout << "\n传长度构造:" << std::endl;
Array<int> arr(12);
int num = 1;
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
arr[i] = num++;
}
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
std::cout << "\n正向迭代器遍历:" << std::endl;
for (Array<int>::Iterator it = arr.begin(); it != arr.end(); it++) {
std::cout << *it << ", ";
}
std::cout << std::endl;
std::cout << "\n正向常量迭代器遍历:" << std::endl;
for (Array<int>::ConstIterator it = arr.cbegin(); it != arr.cend(); it++) {
std::cout << *it << ", ";
}
std::cout << std::endl;
std::cout << "\n反向迭代器遍历:" << std::endl;
for (Array<int>::ReverseIterator it = arr.rbegin(); it != arr.rend(); it++) {
std::cout << *it << ", ";
}
std::cout << std::endl;
std::cout << "\n反向常量迭代器遍历:" << std::endl;
for (Array<int>::ConstReverseIterator it = arr.crbegin(); it != arr.crend(); it++) {
std::cout << *it << ", ";
}
std::cout << std::endl;
std::cout << "\n正向迭代器修改值:" << std::endl;
for (Array<int>::Iterator it = arr.begin(); it != arr.end(); it++) {
*it = 7;
}
for (Array<int>::Iterator it = arr.begin(); it != arr.end(); it++) {
std::cout << *it << ", ";
}
std::cout << std::endl;
std::cout << "\n反向迭代器修改值:" << std::endl;
for (Array<int>::ReverseIterator it = arr.rbegin(); it != arr.rend(); it++) {
*it = num++;
}
for (Array<int>::ReverseIterator it = arr.rbegin(); it != arr.rend(); it++) {
std::cout << *it << ", ";
}
std::cout << std::endl;
for (Array<int>::Iterator it = arr.begin(); it != arr.end(); it++) {
std::cout << *it << ", ";
}
std::cout << std::endl;
std::cout << "\n迭代器随机访问测试:" << std::endl;
auto it = arr.begin();
std::cout << *it << std::endl;
it = it + 5;
std::cout << *it << std::endl;
}
6.总结
特殊的测试没有做,不知道有没有问题。
|