1、手动实现vector
- #include
- using namespace std;
-
- template<typename T>
- class myVector
- {
- private:
- T* arr;
- size_t size; //元素个数
- size_t capacity; //数组容量
- public:
- //构造函数
- myVector() :arr(nullptr), size(0), capacity(0) {}
- myVector(int size)
- {
- if (size > 0)
- {
- this->capacity = size;
- this->arr = new T[capacity];
- }
- if (size < 0)
- {
- throw double(1);
- }
- }
- myVector(const myVector& other) :size(other.size), capacity(other.capacity)
- {
- if (this != &other)
- {
- arr = new T[capacity];
- for (size_t i = 0; i < size; i++)
- {
- arr[i] = other.arr[i];
- }
- }
- }
- //析构函数
- ~myVector()
- {
- delete[] arr;
- }
- //获取当前元素的个数
- size_t getSize()const
- {
- return size;
- }
- //获取当前容量
- size_t getcapacity()const
- {
- return capacity;
- }
- //返回最后一个元素
- T back()const
- {
-
- return arr[size - 1];
- }
-
- //判空
- bool isEmpty()
- {
- return size == 0;
- }
-
- //尾插
- void push_back(const T& value)
- {
- if (size >= capacity)
- {
- //进行扩容
- size_t new_max_size = (capacity == 0) ? 1 : capacity * 2;
- T* newArr = new T[new_max_size];
- for (size_t i = 0; i < size; i++)
- {
- newArr[i] = arr[i];
- }
- delete[] arr;
- arr = newArr;
- capacity = new_max_size;
- }
- arr[size++] = value;
- }
- //返回指定位置的元素
- T& operator[](size_t index)
- {
- if (index >= size)
- {
- throw double(3);
- }
- return arr[index];
- }
-
- //删除最后一个元素
- 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 < capacity / 4)
- {
- size_t new_max_size = capacity / 2;
- T* newArr = new T[new_max_size];
- for (size_t i = 0; i < size; ++i)
- {
- newArr[i] = arr[i];
- }
- delete[] arr;
- arr = newArr;
- capacity = 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 > capacity)
- {
- // 如果新大小大于容量,需要重新分配内存
- size_t new_capacity = (newSize > capacity* 2) ? newSize : capacity * 2;
- T* newArr = new T[new_capacity];
- for (size_t i = 0; i < size; ++i)
- {
- newArr[i] = arr[i];
- }
- delete[] arr;
- arr = newArr;
- capacity = new_capacity;
- }
- 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<<"当前容器的容量为:"<
getcapacity()< -
- // 测试 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<<"当前容器的容量为:"<
getcapacity()< -
- }
- catch (double e) {
- cout << "Error: " << e << endl;
- }
-
- return 0;
- }
-
-
二、思维导图
-
相关阅读:
CSS便捷小工具
Docker pull Images遇到的问题解决
数据结构--树
tomcat (SCI)ServletContainerInitializer 的加载原理
C++:使用cin.getline()输入超过规定字符数
面试算法题之旋转置换,旋转跳跃我闭着眼
华纳云:负载均衡服务器的作用是什么 有哪些方式
FJSP:小龙虾优化算法(Crayfsh optimization algorithm,COA)求解柔性作业车间调度问题(FJSP),提供MATLAB代码
【红队】ATT&CK - 文件隐藏
java计算机毕业设计基于安卓/Android/微信小程序的校园外卖点餐APP
-
原文地址:https://blog.csdn.net/Lychee_z23/article/details/132890150