MyVector模板类
MyVector.hpp
#pragma once using namespace std; template<class T> class MyVector { public: MyVector(): m_Capacity(0), m_Size(0), pAddress(NULL) { } //构造函数 MyVector(int capacity): m_Capacity(capacity), m_Size(0), pAddress(new T[this->m_Capacity]) { } //析构函数 ~MyVector() { if (this->pAddress != NULL) { delete[] this->pAddress; this->pAddress = NULL; //小心野指针 } } //拷贝构造(防止浅拷贝) MyVector(const MyVector& arr) { this->m_Capacity = arr.m_Capacity; this->m_Size = arr.m_Size; this->pAddress = new T[arr.m_Capacity];//深拷贝 for (int i = 0; i < this->m_Size; i++) { this->pAddress[i] = arr.pAddress[i];//把数组中的元素也拷贝过来 } } //重载等号(防止浅拷贝) MyVector& operator=(const MyVector& arr) { //要先判断原来堆区是否有数据,如果有,要先释放 if (this->pAddress != NULL) { delete[] this->pAddress; this->pAddress = NULL; this->m_Capacity = 0; this->m_Size = 0; } //然后在做深拷贝 this->m_Capacity = arr.m_Capacity; this->m_Size = arr.m_Size; this->pAddress = new T[arr.m_Capacity];//深拷贝 for (int i = 0; i < this->m_Size; i++) { this->pAddress[i] = arr.pAddress[i];//把数组中的元素也拷贝过来 } //返回自身 return *this; } //尾插法 void push_back(const T& val) { //先判断容量 if (this->m_Size == this->m_Capacity) { return; } this->pAddress[this->m_Size] = val; this->m_Size ++; } //尾删法 void pop_back() { if (this->m_Size == 0) { return; } this->m_Size--; } //下标访问 T& operator[](int id) { return this->m_Capacity[id]; //引用返回左值 } //返回容量 int getCapacity() { return this->m_Capacity; } //返回大小 int size() { return this->m_Size; } private: T* pAddress; //开在堆区的数组 int m_Capacity; int m_Size; };
最终版MyVector.hpp
用到了类模板、重载运算符、深拷贝、列表初始化容器std::initializer_list<>
//#include"MyVector.hpp" #pragma once using namespace std; template<class T> class MyVector { public: MyVector(): m_Capacity(0), m_Size(0), pAddress(NULL) { } //构造函数 MyVector(int capacity): m_Capacity(capacity), m_Size(0), pAddress(new T[this->m_Capacity]) { } //实现列表初始化 MyVector(std::initializer_list<T> list): m_Capacity(list.size() * 2), //大小先开两倍 m_Size(0), pAddress(new T[this->m_Capacity]) { for (auto it = list.begin(); it != list.end(); ++it) { this->push_back(*it); } } //析构函数 ~MyVector() { if (this->pAddress != NULL) { delete[] this->pAddress; this->pAddress = NULL; //小心野指针 } } //拷贝构造(防止浅拷贝) MyVector(const MyVector& arr) { this->m_Capacity = arr.m_Capacity; this->m_Size = arr.m_Size; this->pAddress = new T[arr.m_Capacity];//深拷贝 for (int i = 0; i < this->m_Size; i++) { this->pAddress[i] = arr.pAddress[i];//把数组中的元素也拷贝过来 } } //重载等号(防止浅拷贝) MyVector& operator=(const MyVector& arr) { //要先判断原来堆区是否有数据,如果有,要先释放 if (this->pAddress != NULL) { delete[] this->pAddress; this->pAddress = NULL; this->m_Capacity = 0; this->m_Size = 0; } //然后在做深拷贝 this->m_Capacity = arr.m_Capacity; this->m_Size = arr.m_Size; this->pAddress = new T[arr.m_Capacity];//深拷贝 for (int i = 0; i < this->m_Size; i++) { this->pAddress[i] = arr.pAddress[i];//把数组中的元素也拷贝过来 } //返回自身 return *this; } //尾插法 void push_back(const T& val) { //先判断容量 if (this->m_Size == this->m_Capacity) { return; } this->pAddress[this->m_Size] = val; this->m_Size ++; } //尾删法 void pop_back() { if (this->m_Size == 0) { return; } this->m_Size--; } //下标访问 T& operator[](int id) { return this->pAddress[id]; //引用返回左值 } //返回容量 int getCapacity() { return this->m_Capacity; } //返回大小 int size() { return this->m_Size; } private: int m_Capacity; int m_Size; T* pAddress; //开在堆区的数组 //放后面,先初始化大小,在初始化堆区的数组! }; int main() { //列表初始化 MyVector<int> id = { 1,2,3,4,5 }; for (int i = 0; i < id.size(); i++) { cout << id[i] << endl; } MyVector<string> name3 = { "AAA","BBB","CCC","DDD" }; for (int i = 0; i < name3.size(); i++) { cout << name3[i] << endl; } cout << "--------------------" << endl; //直接赋值 MyVector<string> names1, names2(10); names2.push_back("EEE"); names2.push_back("FFF"); names2.push_back("GGG"); names2.push_back("MMM"); names1 = names2; for (int i = 0; i < names1.size(); i++) { cout << names1[i] << endl; } for (int i = 0; i < names1.size(); i++) { cout << names2[i] << endl; } return 0; }

浙公网安备 33010602011771号