<泛> STL - vector 模拟实现
今天为大家带来一个模拟STL-vector的模板实现代码。
首先看一下测试结果,之后再为大家呈现设计
测试效果
测试代码
#include<iostream> #include<vector> #include"myVector.h" using namespace std; int main(int argc, char* argv[]) { vector<int>v; vector<int>::iterator it, it2, it3; myVector<int>V; myVector<int>::myIterator It, It2, It3; //push_back 测试 for (int i = 0; i < 10; ++i) v.push_back(i), V.push_back(i); cout << endl << "push_back 模拟测试,以及对C++11的《基于范围的for循环》进行兼容,效果如下:" << endl << endl; cout << "STL: "; for (auto i : v)cout << i << " "; cout << endl << endl; cout << "my: "; for (auto i : V)cout << i << " "; cout << endl << endl; cout << "*--------------------------------------------------------------------------------------------*" << endl << endl; cout << "容器的容量增长规律检测,效果如下:" << endl << endl; cout << "STL: 当前最大容量为 " << v.capacity() << endl << endl; cout << "my: 当前最大容量为 " << V.capacity() << endl << endl; cout << "*-----------------------------------------------------------------------------------------------*" << endl << endl; //erase 测试 迭代器 + 测试 begin() end()函数测试 cout << "erase 测试、迭代器的运算测试,以及begin()、end()函数测试,效果如下" << endl << endl; it = v.erase(v.begin() + 2, v.begin() + 5); cout << "STL: 删除区间【2,5):"; for (auto i : v)cout << i << " "; cout << endl << endl; cout << "当前迭代器位置:" << it - v.begin() << endl << endl; It = V.erase(V.begin() + 2, V.begin() + 5); cout << "my: 删除区间【2,5):"; for (auto i : V)cout << i << " "; cout << endl << endl; cout << "当前迭代器位置:" << It - V.begin() << endl << endl; it = v.erase(v.begin() + 5); cout << "STL: 单点删除【5】 :"; for (auto i : v)cout << i << " "; cout << endl << endl; cout << "当前迭代器位置:" << it - v.begin() << endl << endl; It = V.erase(V.begin() + 5); cout << "my: 单点删除【5】:"; for (auto i : V)cout << i << " "; cout << endl << endl; cout << "当前迭代器位置:" << It - V.begin() << endl << endl; cout << "*-----------------------------------------------------------------------------------------------*" << endl << endl; //insert测试 it = v.insert(v.begin() + 1, v.end() - 3, v.end()); cout << "STL: 区间插入:【1】处插入【" << v.end() - 3 - v.begin() << "," << v.end() - v.begin() << "):,效果如下:" << endl << endl; for (auto i : v)cout << i << " "; cout << endl << endl; cout << "当前迭代器位置:" << it - v.begin() << endl << endl; It = V.insert(V.begin() + 1, V.end() - 3, V.end()); cout << "my: 区间插入:【1】处插入【" << V.end() - 3 - V.begin() << "," << V.end() - V.begin() << "):,效果如下:" << endl << endl; for (auto i : V)cout << i << " "; cout << endl << endl; cout << "当前迭代器位置:" << It - V.begin() << endl << endl; it = v.insert(v.begin() + 4, 100); cout << "STL: 单点插入:【4】处插入100,效果如下:" << endl << endl; for (auto i : v)cout << i << " "; cout << endl << endl; cout << "当前迭代器位置:" << it - v.begin() << endl << endl; It = V.insert(V.begin() + 4, 100); cout<<"my: 单点插入:【4】处插入100,效果如下:" << endl << endl; for (auto i : V)cout << i << " "; cout << endl << endl; cout << "当前迭代器位置:" << It - V.begin() << endl << endl; it = v.insert(v.begin() + 3, 5, 99); cout << "STL: 单点插入:【3】处插入5个99,效果如下:" << endl << endl; for (auto i : v)cout << i << " "; cout << endl << endl; cout << "当前迭代器位置:" << it - v.begin() << endl << endl; It = V.insert(V.begin() + 3, 5, 99); cout << "my: 单点插入:【3】处插入5个99,效果如下:" << endl << endl; for (auto i : V)cout << i << " "; cout << endl << endl; cout << "当前迭代器位置:" << It - V.begin() << endl << endl; cout << "*-----------------------------------------------------------------------------------------------*" << endl << endl; //assign 测试 cout << "assign 测试,效果如下:" << endl << endl; cout << "STL: assign区间参数重载,用原来的vector区间设置新的vector,效果如下:" << endl << endl; vector<int> v_; v_.assign(v.begin() + 2, v.begin() + 8); cout << "新容器v_,用原来的【2,8)设置容器元素内容: "; for (auto i : v)cout << i << " "; cout << endl << endl; myVector<int> V_; V_.assign(V.begin() + 2, V.begin() + 8); cout << "my: assign区间参数重载,用原来的vector区间设置新的vector,效果如下:" << endl << endl; cout << "新容器V_,用原来的【2,8)设置容器元素内容: "; for (auto i : V)cout << i << " "; cout << endl << endl; cout << "STL: assign另一个重载,将原测试容器内容设置为5个10,效果如下:" << endl << endl; v.assign(5, 10); for (auto i : v)cout << i << " "; cout << endl << endl; cout << "my: assign另一个重载,将原测试容器内容设置为5个10,效果如下:" << endl << endl; V.assign(5, 10); for (auto i : V)cout << i << " "; cout << endl << endl; cout << "*-----------------------------------------------------------------------------------------------*" << endl << endl; //pop 测试 cout << "STL: 弹出最后两个元素,然后push一个88,效果如下:" << endl << endl; v.pop_back(), v.pop_back(), v.push_back(88); for (auto i : v)cout << i << " "; cout << endl << endl; cout << "front()求取容器的第一个元素为:" << v.front() << endl << endl; cout << "back()求取容器的最后一个元素为:" << v.back() << endl << endl; cout << "my: 弹出最后两个元素,然后插入push一个88,效果如下:" << endl << endl; V.pop_back(), V.pop_back(), V.push_back(88); for (auto i : V)cout << i << " "; cout << endl << endl; cout << "front()求取容器的第一个元素为:" << V.front() << endl << endl; cout << "back()求取容器的最后一个元素为:" << V.back() << endl << endl; cout << "*-----------------------------------------------------------------------------------------------*" << endl << endl; //索引操作 cout << "清空容器,重置为:" << endl << endl; v.clear(), V.clear(); for (int i = 0; i < 10; ++i) v.push_back(i), V.push_back(i); cout << "STL: "; for (auto i : v)cout << i << " "; cout << endl << endl; cout << "my: "; for (auto i : V)cout << i << " "; cout << endl << endl; cout << "STL: 【8】为:" << v[8] << endl << endl; cout << "my: 【8】为:" << V[8] << endl << endl; // cout << "my: at(12)为:" << V.at(12) << endl << endl; //两者同样抛出错误 // cout << "STL: at(12)为:" << v.at(12) << endl << endl; cout << "*-------------------------------------------- E N D --------------------------------------------*" << endl << endl; }
测试结果
push_back 模拟测试,以及对C++11的《基于范围的for循环》进行兼容,效果如下: STL: 0 1 2 3 4 5 6 7 8 9 my: 0 1 2 3 4 5 6 7 8 9 *--------------------------------------------------------------------------------------------* 容器的容量增长规律检测,效果如下: STL: 当前最大容量为 13 my: 当前最大容量为 13 *-----------------------------------------------------------------------------------------------* erase 测试、迭代器的运算测试,以及begin()、end()函数测试,效果如下 STL: 删除区间【2,5):0 1 5 6 7 8 9 当前迭代器位置:2 my: 删除区间【2,5):0 1 5 6 7 8 9 当前迭代器位置:2 STL: 单点删除【5】 :0 1 5 6 7 9 当前迭代器位置:5 my: 单点删除【5】:0 1 5 6 7 9 当前迭代器位置:5 *-----------------------------------------------------------------------------------------------* STL: 区间插入:【1】处插入【6,9):,效果如下: 0 6 7 9 1 5 6 7 9 当前迭代器位置:1 my: 区间插入:【1】处插入【6,9):,效果如下: 0 6 7 9 1 5 6 7 9 当前迭代器位置:1 STL: 单点插入:【4】处插入100,效果如下: 0 6 7 9 100 1 5 6 7 9 当前迭代器位置:4 my: 单点插入:【4】处插入100,效果如下: 0 6 7 9 100 1 5 6 7 9 当前迭代器位置:4 STL: 单点插入:【3】处插入5个99,效果如下: 0 6 7 99 99 99 99 99 9 100 1 5 6 7 9 当前迭代器位置:3 my: 单点插入:【3】处插入5个99,效果如下: 0 6 7 99 99 99 99 99 9 100 1 5 6 7 9 当前迭代器位置:3 *-----------------------------------------------------------------------------------------------* assign 测试,效果如下: STL: assign区间参数重载,用原来的vector区间设置新的vector,效果如下: 新容器v_,用原来的【2,8)设置容器元素内容: 0 6 7 99 99 99 99 99 9 100 1 5 6 7 9 my: assign区间参数重载,用原来的vector区间设置新的vector,效果如下: 新容器V_,用原来的【2,8)设置容器元素内容: 0 6 7 99 99 99 99 99 9 100 1 5 6 7 9 STL: assign另一个重载,将原测试容器内容设置为5个10,效果如下: 10 10 10 10 10 my: assign另一个重载,将原测试容器内容设置为5个10,效果如下: 10 10 10 10 10 *-----------------------------------------------------------------------------------------------* STL: 弹出最后两个元素,然后push一个88,效果如下: 10 10 10 88 front()求取容器的第一个元素为:10 back()求取容器的最后一个元素为:88 my: 弹出最后两个元素,然后插入push一个88,效果如下: 10 10 10 88 front()求取容器的第一个元素为:10 back()求取容器的最后一个元素为:88 *-----------------------------------------------------------------------------------------------* 清空容器,重置为: STL: 0 1 2 3 4 5 6 7 8 9 my: 0 1 2 3 4 5 6 7 8 9 STL: 【8】为:8 my: 【8】为:8 *-------------------------------------------- E N D --------------------------------------------* 请按任意键继续. . .
template实现代码
新版本:
因为最近写了myStack,需要基于myVector实现,所以,把myVector重新完善了一下,同时也添加了一些注释,增加了一些功能,优化了一些内容
如下:
#ifndef _MY_VECTOR #define _MY_VECTOR template<class T> class myVector { public: //public the date-type information used by class design
typedef myVector<T> _Mytype; typedef size_t size_type; typedef T value_type; typedef T* _pointer; #define self (*this) public: struct myIterator { _pointer pIt; myIterator(const _pointer p = nullptr) :pIt(p) { } T operator*() { //迭代器解引用 return *pIt; } myIterator& operator=(const myIterator& other) { //迭代器赋值 if (this != &other) pIt = other.pIt; return self; } myIterator operator+(int n) { //迭代器移动,不改变原值 return myIterator(pIt + n); } myIterator operator-(int n) { return self + (-n); } myIterator& operator++() { //迭代器 前置++ pIt++; return self; } myIterator operator++(int) { //后置++ myIterator t(self); self.pIt++; return t; } bool operator!=(const myIterator& rhs)const { return pIt != rhs.pIt; } int operator-(const myIterator& rhs)const { //迭代器作差 return pIt - rhs.pIt; } }; public: //迭代器相关函数 myIterator end() { //取逾尾指针 return myIterator(pBuff + len); } myIterator begin() { return myIterator(pBuff); } myIterator erase(const myIterator& first, const myIterator& last) //@brief erase 【first,last) //@retur the new Iterator of first position { int from{ first.pIt - pBuff }; int to{ last.pIt - pBuff }; for (int i = from; to < len; ++to) pBuff[i++] = pBuff[to]; len -= last - first; return myIterator(pBuff + from); } myIterator erase(const myIterator& pos) //@brief erase the value of pos position //@retur the new Iterator of pos position { int index{ pos.pIt - pBuff }; if (index == len) throw "out of range!"; for (int i = index; i < len; ++i) pBuff[i] = pBuff[i + 1]; len--; return myIterator(pBuff + index); } myIterator insert(const myIterator& pos, const myIterator& first, const myIterator& last) //@brief insert [first,last) into pos position //@retur the new Iterator of pos position { int index{ pos.pIt - pBuff }; int from{ first.pIt - pBuff }; int n{ last - first }; resize(len + n); for (int i = len - n - 1; i >= index; --i) pBuff[i + n] = pBuff[i]; for (int i = index; i < index + n; ++i) pBuff[i] = pBuff[n + from++]; return myIterator(pBuff + index); } myIterator insert(const myIterator& pos, size_type n, const T& val) //@brief insert n vals into pos position //@retur the new iterator of pos position { int index{ pos.pIt - pBuff }; resize(len + n); for (int i = len - n - 1; i >= index; --i) pBuff[i + n] = pBuff[i]; for (int i = index; i < index + n; ++i) pBuff[i] = val; return myIterator(pBuff + index); } myIterator insert(const myIterator& pos, const T& val) //@brief insert the val into pos position //@retur the new Iterator of pos position { int index{ pos.pIt - pBuff }; _Check(); for (int i = len - 1; i >= index; --i) pBuff[i + 1] = pBuff[i]; pBuff[index] = val; len++; return myIterator(pBuff + index); } public: //some basic funciotns of vector class myVector() :len(0), maxSize(0), pBuff(nullptr) { //null vector } myVector(const size_type n, const T& val) :len(n) ,maxSize(n) ,pBuff(new T[maxSize]) { // construct a vector with n vals for (int i = 0; i < len; ++i) pBuff[i] = val; } myVector(const size_type n) :len(n) ,maxSize(n) ,pBuff(new T[maxSize]) { // construct a vector which contains n elements } myVector(myIterator first, myIterator last) :len(last - first) , maxSize(len) , pBuff(new T[len]) { for (int i = 0; first != last; ++first, ++i) pBuff[i] = *first; } myVector(const _Mytype& other) :len(other.len) ,maxSize(other.maxSize) ,pBuff(nullptr) { if (other.pBuff != nullptr) { pBuff = new T[maxSize]; memcpy(pBuff, other.pBuff, sizeof(T)*len); } } ~myVector() { clear(); } public: size_type size()const { //the number of vector's elements return len; } bool empty()const { //weather vector is null or not return !len; } size_type capacity()const { //the capacity of vector return maxSize; } public: // the operator-loading functions of vector bool operator==(const _Mytype& rhs)const { if (len != rhs.size()) return false; for (int i = 0; i < len; ++i) if (pBuff[i] != rhs[i]) return false; return true; } bool operator!=(const _Mytype& rhs)const { return !(self == rhs); } bool operator<(const _Mytype& rhs)const { if (empty())return true; size_type n = (len < rhs.size()) ? len : rhs.len; for (int i = 0; i < n; ++i) { if (pBuff[i] < rhs.pBuff[i])return true; if (pBuff[i] > rhs.pBuff[i])return false; } return false; } bool operator<=(const _Mytype& rhs)const { return self < rhs || self == rhs; } bool operator>(const _Mytype& rhs)const { return !(self <= rhs); } bool operator>=(const _Mytype& rhs)const { return !(self < rhs); } _Mytype& operator=(const _Mytype& rhs) { if (this != &rhs) { resize(rhs.size()); for (int i = 0; i < len; ++i) pBuff[i] = rhs[i]; } return self; } T& operator[](const size_type index) { return pBuff[index]; } const T& operator[](const size_type index)const { return pBuff[index]; } public: //the main option of vector void push_back(const T& val) { _Check(); pBuff[len++] = val; } void pop_back() { //erase the last value of vector len--; } void resize(const size_type n) { //reset the number of vector's elements if (n >= maxSize) { maxSize = n; _pointer tbuff = new T[maxSize]; memcpy(tbuff, pBuff, sizeof(T)*len); if (pBuff) delete[] pBuff; pBuff = tbuff; } len = n; } void swap(_Mytype& rhs) { //交互两个vector _pointer tbuff = pBuff; size_type tlen = len; size_type tmaxsize = maxSize; //上述三行代码不能用构造函数代替,即:不可以myVerter t(self) pBuff = rhs.pBuff; len = rhs.len; maxSize = rhs.maxSize; rhs.pBuff = tbuff; rhs.len = tlen; rhs.maxSize = tmaxsize; } void assign(const size_type n, const T& val) { //reset the vector self = myVector(n, val); } void assign(myIterator first, myIterator last) { //reset tje vector self = myVector(first, last); } T& at(const size_type index) { //the element of index position if (index < 0 || index >= len) throw "range_out"; return pBuff[index]; } T front() { //get the first element of vector return pBuff[0]; } T back() { //get the last element of vector return pBuff[len - 1]; } const T& back()const { return pBuff[len - 1]; } void clear() { //clear the vector if (pBuff) delete[] pBuff; pBuff = nullptr; maxSize = len = 0; } private: size_type len, maxSize; //当前长度以及最大长度(已开辟) T* pBuff; void _Check() //@brief Guarantee the relationship between len and maxSize is correct //@notice if len>=maxSize maxSize += maxSize / 2 { while (len >= maxSize) { maxSize = maxSize + ((maxSize >> 1) > 1 ? (maxSize >> 1) : 1); T* tbuff = new T[maxSize]; memcpy(tbuff, pBuff, sizeof(T)*len); if (pBuff) delete[] pBuff; pBuff = tbuff; } } }; template<typename T> void swap(myVector<T>& lhs, myVector<T>& rhs) { lhs.swap(rhs); } #endif
旧版本:
#pragma once template<typename T> class myVector { size_t len, maxSize; //当前长度以及最大长度(已开辟) T* pBuff; public: struct myIterator { T* pIt; T operator*() //迭代器解引用 { return *pIt; } myIterator& operator=(const myIterator& other) { if (this != &other) pIt = other.pIt; return *this; } myIterator operator+(int n) { myIterator it; it.pIt = pIt; it.pIt += n; return it; } myIterator operator-(int n) { return *this + (-n); } myIterator& operator++() //前置++ { pIt++; return *this; } bool operator!=(const myIterator& rhs)const { return pIt != rhs.pIt; } size_t operator-(const myIterator& rhs)const //迭代器相减 { return pIt - rhs.pIt; } }; public: myIterator end() { myIterator it; it.pIt = pBuff + len; return it; } myIterator begin() { myIterator it; it.pIt = pBuff; return it; } myIterator erase(const myIterator& first, const myIterator& last) //区间删除 { int index1 = first.pIt - pBuff; int index2 = last.pIt - pBuff; for (int x = index1, i = index2; i < len; ++i) pBuff[x++] = pBuff[i]; //区间均为前闭后开 len -= last - first; myIterator tIt; tIt.pIt = pBuff + index1; return tIt; } myIterator erase(const myIterator& pos) { int index = pos.pIt - pBuff; if (index == len) throw "out of range!"; for (int i = index; i < len - 1; ++i) pBuff[i] = pBuff[i + 1]; len--; myIterator tIt; tIt.pIt = pBuff + index; return tIt; } myIterator insert(const myIterator& pos, const myIterator& first, const myIterator& last) { int index = pos.pIt - pBuff; int fir = first.pIt - pBuff; int n = last - first; resize(len + n); for (int i = len - n - 1; i >= index; --i) pBuff[i + n] = pBuff[i]; for (int i = index; i < index + n; ++i) pBuff[i] = pBuff[n + fir++]; myIterator it; it.pIt = pBuff + index; return it; } myIterator insert(const myIterator& pos, int n, const T& val) { int index = pos.pIt - pBuff; resize(len + n); for (int i = len - n - 1; i >= index; --i) //移动元素 pBuff[i + n] = pBuff[i]; for (int i = index; i < index + n; ++i) //插入元素 pBuff[i] = val; myIterator it; it.pIt = pBuff + index; return it; } myIterator insert(const myIterator& pos, const T&val) { int index = pos.pIt - pBuff; if (len >= maxSize) { maxSize = maxSize + ((maxSize >> 1)>1 ? (maxSize >> 1) : 1); T* tbuff = new T[maxSize]; memcpy(tbuff, pBuff, sizeof(T)* len); if (pBuff) delete[] pBuff; pBuff = tbuff; } for (int i = len - 1; i >= index; --i) pBuff[i + 1] = pBuff[i]; pBuff[index] = val; len++; myIterator it; it.pIt = pBuff + index; return it; } public: myVector(const int, const T&); //生成一个容器,容器中含有para1个para2元素 myVector(const int); //生成大小为para1的容器 myVector(const myVector&); myVector(myIterator first, myIterator last); //迭代器区间构造 myVector(); //default-constructor ~myVector(); void clear(); public: size_t size()const; bool empty()const; size_t capacity()const; //最大容量 public: bool operator==(const myVector&)const; bool operator!=(const myVector&)const; bool operator<(const myVector&)const; bool operator<=(const myVector&)const; bool operator>(const myVector&)const; bool operator>=(const myVector&)const; myVector& operator=(const myVector&); T& operator[](const int); //索引符重载 const T& operator[](const int)const; public: void swap(const myVector&); void assign(const int n, const T& val); //重置里面的值 void assign(myIterator first, myIterator last); T& at(const int); //同索引符,但会检查越界,抛出异常 T front(); //返回第一个元素的位置。 T back(); //返回最后一个元素的位置 public: void push_back(const T&); void pop_back(); void resize(int); //重置元素数量 }; template<typename T> myVector<T>::myVector(int n, const T& val) { len = maxSize = n; pBuff = new T[maxSize]; for (int i = 0; i < len; ++i) pBuff[i] = val; } template<typename T> myVector<T>::myVector(int n) :len(n), maxSize(n), pBuff(new T[maxSize]) { } template<typename T> myVector<T>::myVector(myIterator first, myIterator last) { int n = last - first; if (n > 0) { maxSize = len = n; pBuff = new T[n]; for (int i = 0; i < n; ++i) pBuff[i] = *(first + i); } } template<typename T> myVector<T>::myVector() :len(0), maxSize(0), pBuff(nullptr) { } template<typename T> myVector<T>::myVector(const myVector& other) :len (other.len), maxSize (other.maxSize), pBuff (nullptr) { if (other.pBuff != nullptr) { pBuff = new T[maxSize]; memcpy(pBuff, other.pBuff, sizeof(T)*len); } } template<typename T> void myVector<T>::clear() { if (pBuff) delete[] pBuff; pBuff = nullptr; maxSize = len = 0; } template<typename T> myVector<T>::~myVector() { clear(); } template<typename T> size_t myVector<T>::size()const { return len; } template<typename T> bool myVector<T>::empty()const { return !len; } template<typename T> size_t myVector<T>::capacity()const { return maxSize; } template<typename T> bool myVector<T>::operator==(const myVector& rhs)const { if (len != rhs.size()) return false; for (int i = 0; i < len; ++i) //如果两者大小一样,那么就判断里面的元素 (可能不需要) if (pBuff[i] != rhs[i]) return false; return true; } template<typename T> bool myVector<T>::operator<(const myVector& rhs)const { if (empty())return true; //如果左操作数为空,则直接返回真 int n = (len < rhs.size()) ? len : rhs.len; for (int i = 0; i < n; ++i) { if (pBuff[i] < rhs.pBuff[i])return true; if (pBuff[i] > rhs.pBuff[i])return false; } return false; } template<typename T> bool myVector<T>::operator<=(const myVector& rhs)const { return *this<rhs || *this == rhs; } template<typename T> bool myVector<T>::operator>(const myVector& rhs)const { return !(*this <= rhs); } template<typename T> bool myVector<T>::operator>=(const myVector& rhs)const { return !(*this < rhs); } template<typename T> bool myVector<T>::operator!=(const myVector& rhs)const { return !(*this == rhs); } template<typename T> myVector<T>& myVector<T>::operator=(const myVector& rhs) { if (this != &rhs) { resize(rhs.size()); for (int i = 0; i < len; ++i) pBuff[i] = rhs[i]; } return *this; } template<typename T> void myVector<T>::assign(const int n, const T& val) { *this = myVector(n, val); } template<typename T> void myVector<T>::assign(myIterator first, myIterator last) { *this = myVector(first, last); } template<typename T> T& myVector<T>::at(const int index) { if (index < 0 || index >= len) throw "range_out"; return pBuff[index]; } template<typename T> T& myVector<T>::operator[](int index) { return pBuff[index]; } template<typename T> const T& myVector<T>::operator[](int index)const { return pBuff[index]; } template<typename T> void myVector<T>::swap(const myVector& rhs) { T* tbuff = pBuff; size_t tlen = len; size_t tmaxsize = maxSize; pBuff = rhs.pBuff; len = rhs.len; maxSize = rhs.maxSize; rhs.pBuff = tbuff; rhs.len = tlen; rhs.maxSize = tmaxsize; } template<typename T> T myVector<T>::front() { return pBuff[0]; } template<typename T> T myVector<T>::back() { return pBuff[len - 1]; } template<typename T> void myVector<T>::push_back(const T& val) { if (len >= maxSize) { maxSize = maxSize + ((maxSize >> 1) > 1 ? (maxSize >> 1) : 1); T* tbuff = new T[maxSize]; for (int i = 0; i < len; ++i) tbuff[i] = pBuff[i]; delete[] pBuff; pBuff = tbuff; } pBuff[len++] = val; } template<typename T> void myVector<T>::pop_back() { len--; } template<typename T> void myVector<T>::resize(int n) { if (n >= maxSize) { maxSize = n; T* tbuff = new T[maxSize]; memcpy(tbuff, pBuff, sizeof(T)*len); if (pBuff) delete[] pBuff; pBuff = tbuff; } len = n; }
有一些功能没有,但是都类似,自己添加就好了。
感谢您的阅读,生活愉快~
作者:林-兮
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。