#include <iostream>
using namespace std;
template<typename T>
class myVector
{
private:
T* arr;
size_t size; //元素数量
size_t max_size; //数组容量
public:
//构造函数
myVector() :arr(nullptr), size(0), max_size(0) {}
myVector(int size)
{
if (size > 0)
{
this->max_size = size;
this->arr = new T[max_size];
this->size = 0;
cout << "容量设置成功" << endl;
}
if (size < 0)
{
throw double(1);
}
}
myVector(const myVector& other) :size(other.size), max_size(other.max_size)
{
if (this != &other)
{
arr = new T[max_size];
for (size_t i = 0; i < size; i++)
{
arr[i] = other.arr[i];
}
}
}
//析构函数
~myVector()
{
delete[] arr;
}
//获取当前元素的个数
size_t getSize()const
{
return size;
}
//获取当前容量
size_t getMax_size()const
{
return max_size;
}
//返回最后一个元素
T back()const
{
return arr[size - 1];
}
//判空
bool isEmpty()
{
return size == 0;
}
//尾插
void push_back(const T& value)
{
if (size >= max_size)
{
//进行扩容
size_t new_max_size = (max_size == 0) ? 1 : max_size * 2;
T* newArr = new T[new_max_size];
for (size_t i = 0; i < size; i++)
{
newArr[i] = arr[i];
}
delete[] arr;
arr = newArr;
max_size = new_max_size;
}
arr[size++] = value;
}
//返回指定位置的元素
T& operator[](size_t index)
{
if (index >= size)
{
throw double(3);
}
return arr[index];
}
//at函数
T& at(size_t index)const
{
if (index < size)
{
return arr[index];
}
else
{
throw double(4);
}
}
//删除最后一个元素
void pop_back()
{
if (!isEmpty())
{
--size;
}
}
//删除任意元素
void erase(size_t index)
{
if (index >= size)
{
throw double(5);
}
for (size_t i = index; i < size - 1; ++i)
{
arr[i] = arr[i + 1];
}
--size;
//可以减少数组的容量
if (size < max_size / 4)
{
size_t new_max_size = max_size / 2;
T* newArr = new T[new_max_size];
for (size_t i = 0; i < size; ++i)
{
newArr[i] = arr[i];
}
delete[] arr;
arr = newArr;
max_size = new_max_size;
}
}
//返回第一个元素
T front()const
{
return arr[0];
}
//调整容器的大小
void resize(size_t newSize)
{
if (newSize < 0)
{
throw double(7);
}if (newSize == size)
{
return;
}
if (newSize < size)
{
size = newSize;
}
else
{
if (newSize > max_size)
{
// 如果新大小大于容量,需要重新分配内存
size_t new_max_size = (newSize > max_size* 2) ? newSize : max_size * 2;
T* newArr = new T[new_max_size];
for (size_t i = 0; i < size; ++i)
{
newArr[i] = arr[i];
}
delete[] arr;
arr = newArr;
max_size = new_max_size;
}
while (size < newSize)
{
push_back(T());
}
}
}
};
int main() {
try {
size_t size;
cout<<"请设置容器的最大容量:";
cin>>size;
myVector<int> vec(size);
// 添加元素
for (int i = 1; i <= 10; ++i) {
vec.push_back(i);
}
// 访问和打印元素
cout << "当前容器中的元素为: ";
for (size_t i = 0; i < vec.getSize(); ++i) {
cout << vec[i] << " ";
}
cout << endl;
cout<<"当前容器的容量为:"<<vec.getMax_size()<<endl;
// 测试 front 和 back 函数
cout << "第一个元素为: " << vec.front() << endl;
cout << "最后一个元素为: " << vec.back() << endl;
// 删除最后一个元素
vec.pop_back();
// 访问和打印元素
cout << "删除最后一个元素后容器中的元素为: ";
for (size_t i = 0; i < vec.getSize(); ++i) {
cout << vec[i] << " ";
}
cout << endl;
int num;
cout<<"请输入你要删除第几个元素:";
cin>>num;
// 删除指定位置的元素
vec.erase(--num); // 删除第三个元素
// 访问和打印元素
cout << "执行删除后容器中的元素为: ";
for (size_t i = 0; i < vec.getSize(); ++i) {
cout << vec[i] << " ";
}
cout << endl;
// 调整容器大小
int a;
cout<<"请输入你要设置的容器大小:";
cin>>a;
vec.resize(a);
// 访问和打印元素
cout << "调整容器大小后容器中的元素为: ";
for (size_t i = 0; i < vec.getSize(); ++i) {
cout << vec[i] << " ";
}
cout << endl;
cout<<"当前容器的容量为:"<<vec.getMax_size()<<endl;
// 测试 at 函数
cout << "at(2) = " << vec.at(2) << endl;
// 测试越界访问
//cout << vec[20] << endl; // 会抛出异常
}
catch (double e) {
cout << "Error: " << e << endl;
}
return 0;
}
脑图