思维导图:
#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() {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; // 会抛出异常return 0;
}