#include <iostream>
#include<iomanip>
#include<math.h>
#include<algorithm>
using namespace std;
class Array
{
private:
int length;
int largeSize = 100;
int* arr = NULL;
public:
Array(int l)
{
length = l;
if (arr == NULL)
{
arr = new int[largeSize];
}
for (int i = 0; i < length; i++)
{
cin >> arr[i];
}
}
~Array()
{
//arr = arr - 1;
if (arr != NULL)
{
delete[] arr;
arr = NULL;
}
}
void insert(int i, int tar);
void cut(int i);
void print();
};
void Array::insert(int i, int tar)
{
if (length + 1 > largeSize)
{
int* a = new int[length];
for (int j = 0;j < length; j++)
{
a[j] = arr[j];
}
delete[] arr;
largeSize++;
arr = new int[largeSize];
for (int j = 0; j < length; j++)
{
arr[j] = a[j];
}
}
int* q = arr + i - 1;
int* p = arr + length - 1;
for (p; p >= q; p--)
{
*(p + 1) = *p;
}
*q = tar;
length++;
}
void Array::cut(int i)
{
i--;
for (int j = i + 1; j < length; j++)
{
*(arr + j - 1) = *(arr + j);
}
length--;
}
void Array::print()
{
for (int i = 0; i < length; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
int main()
{
int t;
cin >> t;
while (t--)
{
int i, l, tar;
cin >> l;
Array goal(l);
cin >> i >> tar;
goal.insert(i, tar);
//goal.cut(i);
goal.print();
}
return 0;
}
数组元素的添加和删除
最新推荐文章于 2024-11-05 21:58:11 发布