IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> 第91篇 数据结构(一)数组 -> 正文阅读

[C++知识库]第91篇 数据结构(一)数组


本篇主要是说明数组的封装,数组的具体介绍一搜一大把,这里就不一一说明,这里主要是数组数组相关操作的封装实现,代码不复杂,就不作算法介绍了。

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_arrayType*私有存储数据
m_lensize_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() constsize_t-公有获取数据个数
length() constsize_t-公有获取数据个数
setData()voidint index, Type value公有设置某个位置的值
empty() constbool-公有判断是否为空
push_front()boolType value公有头部插入数据
push_back()boolType value公有尾部插入数据
insert()boolint index, Type value公有指定位置插入数据
insert()boolint begin, int end, Type value公有指定区间插入数据
pop_front()int-公有删除头部数据
pop_back()int-公有删除尾部数据
eraseIndex()boolint index公有删除指定位置数据
eraseValue()boolType value公有删除指定数据
eraseIndexValue()boolint index, Type value公有删除指定位置指定数据
eraseSection()boolint begin, int end公有删除指定区间数据
selectIndex()intint val公有顺序查找数据
selectIndexValue()boolint index, Type val公有查找指定位置指定数据
selectSection()boolint begin, int end, Type value公有指定区间查找数据
replaceIndex()boolint index, Type value公有替换指定位置数据
replaceValue()boolType target, Type value公有替换目标数据
replaceSection()boolint begin, int end, Type value公有替换区间数据
reverse()Array-公有整体反转
reverse()Arrayint begin, int end公有局部反转
bubbleSort()Array-公有冒泡排序
selectionSort()Array-公有选择排序
subArray()Arrayint begin, int end截取区间数据
subArray()Arrayint begin公有截取指定位置往后数据
getData()Typeint index公有返回指定位置数据
operator [] ()Type&int index公有返回指定位置数据
at()Typeint index公有返回指定位置数据
operator + ()ArrayArray 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_arrArray*私有使用迭代器的当前数组
m_positionint私有迭代器的位置

2.3.2.方法

方法名返回类型参数属性说明
iterator()--公有缺省构造
iterator()-Array* arr, int position公有传值构造
operator == ()boolconst iterator& itb公有==运算符重载
operator != ()boolconst iterator& itb公有!=运算符重载
operator + ()iteratorint off公有+运算符重载,支持随机访问
operator - ()iteratorint off公有-运算符重载,支持随机访问
operator ++ ()iterator-公有前置++
operator ++ ()iteratorint公有后置++
operator – ()iterator-公有前置–
operator – ()iteratorint公有后置–
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;
	}

	/*
	1数组插入数据算法说明:
	1.头部插入
	2.尾部插入
	3.指定位置插入
	4.指定区间插入
	*/
	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;
	}
	//----插入算法结束

	/*
	2数组删除数据算法说明:
	1.头部删除
	2.尾部删除
	3.指定位置删除
	4.指定位置指定数据删除
	5.指定区间删除
	6.指定数据删除
	*/
	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;
	}
	//----删除算法结束

	/*
	3数组查找数据算法说明:
	1.顺序查找指定数据
	2.指定位置查找指定数据
	3.指定区间查找数据
	*/
	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;
	}
	//----查找算法结束

	/*
	4数组替换数据算法说明:
	1.替换指定位置数据
	2.替换指定目标数据
	3.替换指定区间数据
	*/
	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;
	}
	//----替换算法结束

	/*
	5数组反转数据算法说明:
	1.整体反转
	2.指定区间反转
	*/
	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;
	}
	//----反转算法结束

	/*
	6数组排序算法说明:
	1.冒泡排序
	2.选择排序
	*/
	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;
	}
	//----排序算法结束

	/*
	数组截取数据算法说明:
	1.截取区间数据
	2.截取指定位置往后数据
	*/
	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 //_ARRAY_H_

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.总结

特殊的测试没有做,不知道有没有问题。

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-04-22 18:17:37  更:2022-04-22 18:18:41 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 0:11:58-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码