今天为大家带来一个模拟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 << "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() + 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 当前迭代器位置:7 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 STL: 区间插入:【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实现代码
#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 i = index2; i < len; ++i) pBuff[index1++] = pBuff[i]; //区间均为前闭后开 len -= last - first; myIterator tIt; tIt.pIt = pBuff + index1; return tIt; } myIterator erase(const myIterator& pos) { int index = pos.pIt - pBuff; for (int i = index; i < len; ++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]; memcpy(tbuff, pBuff, sizeof(T)*len); if (pBuff) 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; }
有一些功能没有,但是都类似,自己添加就好了。
感谢您的阅读,生活愉快~