2023-01-06 03:39:19 +08:00
|
|
|
/**
|
2022-11-25 02:04:38 +08:00
|
|
|
* File: array.cpp
|
|
|
|
* Created Time: 2022-11-25
|
|
|
|
* Author: Krahets (krahets@163.com)
|
|
|
|
*/
|
|
|
|
|
2023-04-24 04:11:18 +08:00
|
|
|
#include "../utils/common.hpp"
|
2022-11-27 04:19:16 +08:00
|
|
|
|
2023-08-17 05:13:19 +08:00
|
|
|
/* 随机访问元素 */
|
2023-04-14 03:44:02 +08:00
|
|
|
int randomAccess(int *nums, int size) {
|
2022-11-27 19:07:35 +08:00
|
|
|
// 在区间 [0, size) 中随机抽取一个数字
|
|
|
|
int randomIndex = rand() % size;
|
|
|
|
// 获取并返回随机元素
|
|
|
|
int randomNum = nums[randomIndex];
|
|
|
|
return randomNum;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 扩展数组长度 */
|
2023-04-14 03:44:02 +08:00
|
|
|
int *extend(int *nums, int size, int enlarge) {
|
2022-11-27 19:07:35 +08:00
|
|
|
// 初始化一个扩展长度后的数组
|
2023-04-14 03:44:02 +08:00
|
|
|
int *res = new int[size + enlarge];
|
2022-11-27 19:07:35 +08:00
|
|
|
// 将原数组中的所有元素复制到新数组
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
res[i] = nums[i];
|
|
|
|
}
|
2023-01-14 19:52:11 +08:00
|
|
|
// 释放内存
|
|
|
|
delete[] nums;
|
2022-11-27 19:07:35 +08:00
|
|
|
// 返回扩展后的新数组
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 在数组的索引 index 处插入元素 num */
|
2023-04-14 03:44:02 +08:00
|
|
|
void insert(int *nums, int size, int num, int index) {
|
2022-11-27 19:07:35 +08:00
|
|
|
// 把索引 index 以及之后的所有元素向后移动一位
|
2022-12-20 13:20:20 +08:00
|
|
|
for (int i = size - 1; i > index; i--) {
|
2022-11-27 19:07:35 +08:00
|
|
|
nums[i] = nums[i - 1];
|
|
|
|
}
|
2023-12-02 06:21:34 +08:00
|
|
|
// 将 num 赋给 index 处的元素
|
2022-11-27 19:07:35 +08:00
|
|
|
nums[index] = num;
|
|
|
|
}
|
|
|
|
|
2023-12-02 06:21:34 +08:00
|
|
|
/* 删除索引 index 处的元素 */
|
2023-04-14 03:44:02 +08:00
|
|
|
void remove(int *nums, int size, int index) {
|
2022-11-27 19:07:35 +08:00
|
|
|
// 把索引 index 之后的所有元素向前移动一位
|
|
|
|
for (int i = index; i < size - 1; i++) {
|
|
|
|
nums[i] = nums[i + 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 遍历数组 */
|
2023-04-14 03:44:02 +08:00
|
|
|
void traverse(int *nums, int size) {
|
2022-11-27 19:07:35 +08:00
|
|
|
int count = 0;
|
|
|
|
// 通过索引遍历数组
|
|
|
|
for (int i = 0; i < size; i++) {
|
2023-11-14 21:27:35 +08:00
|
|
|
count += nums[i];
|
2022-11-27 19:07:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 在数组中查找指定元素 */
|
2023-04-14 03:44:02 +08:00
|
|
|
int find(int *nums, int size, int target) {
|
2022-11-27 19:07:35 +08:00
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
if (nums[i] == target)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Driver Code */
|
|
|
|
int main() {
|
|
|
|
/* 初始化数组 */
|
|
|
|
int size = 5;
|
2023-04-14 03:44:02 +08:00
|
|
|
int *arr = new int[size];
|
2022-11-27 19:07:35 +08:00
|
|
|
cout << "数组 arr = ";
|
2023-04-14 03:44:02 +08:00
|
|
|
printArray(arr, size);
|
2022-11-27 19:07:35 +08:00
|
|
|
|
2023-04-14 03:44:02 +08:00
|
|
|
int *nums = new int[size]{1, 3, 2, 5, 4};
|
2022-11-27 19:07:35 +08:00
|
|
|
cout << "数组 nums = ";
|
2023-04-14 03:44:02 +08:00
|
|
|
printArray(nums, size);
|
|
|
|
|
2022-11-27 19:07:35 +08:00
|
|
|
/* 随机访问 */
|
|
|
|
int randomNum = randomAccess(nums, size);
|
|
|
|
cout << "在 nums 中获取随机元素 " << randomNum << endl;
|
2023-04-14 03:44:02 +08:00
|
|
|
|
2022-11-27 19:07:35 +08:00
|
|
|
/* 长度扩展 */
|
|
|
|
int enlarge = 3;
|
2023-01-14 19:52:11 +08:00
|
|
|
nums = extend(nums, size, enlarge);
|
2022-11-27 19:07:35 +08:00
|
|
|
size += enlarge;
|
|
|
|
cout << "将数组长度扩展至 8 ,得到 nums = ";
|
2023-04-14 03:44:02 +08:00
|
|
|
printArray(nums, size);
|
|
|
|
|
2022-11-27 19:07:35 +08:00
|
|
|
/* 插入元素 */
|
|
|
|
insert(nums, size, 6, 3);
|
|
|
|
cout << "在索引 3 处插入数字 6 ,得到 nums = ";
|
2023-04-14 03:44:02 +08:00
|
|
|
printArray(nums, size);
|
2022-11-27 19:07:35 +08:00
|
|
|
|
|
|
|
/* 删除元素 */
|
|
|
|
remove(nums, size, 2);
|
|
|
|
cout << "删除索引 2 处的元素,得到 nums = ";
|
2023-04-14 03:44:02 +08:00
|
|
|
printArray(nums, size);
|
|
|
|
|
2022-11-27 19:07:35 +08:00
|
|
|
/* 遍历数组 */
|
|
|
|
traverse(nums, size);
|
2023-04-14 03:44:02 +08:00
|
|
|
|
2022-11-27 19:07:35 +08:00
|
|
|
/* 查找元素 */
|
|
|
|
int index = find(nums, size, 3);
|
|
|
|
cout << "在 nums 中查找元素 3 ,得到索引 = " << index << endl;
|
|
|
|
|
2023-01-14 19:52:11 +08:00
|
|
|
// 释放内存
|
|
|
|
delete[] arr;
|
|
|
|
delete[] nums;
|
|
|
|
|
2022-11-27 19:07:35 +08:00
|
|
|
return 0;
|
|
|
|
}
|