C++的vector函数,当数据量到达一定程度,清空时就会花费很多时间。打算自己写一个内存管理器,并写一个可变长的数组。下面是我的vector代码:
MyVector.h:
#ifndef __MY_VECTOR_H #define __MY_VECTOR_H #include <stdio.h> #define SafeDelete(_x) { if (_x) { delete (_x); (_x) = NULL; } } #define SafeDeleteArray(_x) { if (_x) { delete [] (_x); (_x) = NULL; } } template <typename T> class MyVector{ public: //默认构造函数 MyVector(){ this->length=0; this->maxlength=10; this->data=new T[this->maxlength]; for (unsigned i=0;i<this->maxlength;i++) { this->data[i]=NULL; } }; //初始化长度 MyVector(unsigned initLength){ if (initLength<5) { printf("warning: vector init_length must more than 5.\n"); initLength=5; } this->length=0; this->maxlength=initLength; this->data=new T[this->maxlength]; for (unsigned i=0;i<this->maxlength;i++) { this->data[i]=NULL; } }; //拷贝构造函数 MyVector(const MyVector& v){ this->length=v.length; this->maxlength=v.maxlength; this->data=new T[this->maxlength]; for (unsigned i=0;i<this->maxlength;i++) { this->data[i]=v.data[i]; } }; ~MyVector(){ this->length=0; this->maxlength=0; SafeDeleteArray(this->data); }; //插入一个元素到最后 void insert(T element){ if (this->length>=this->maxlength) { unsigned i; T* dataTemp=new T[this->maxlength*2]; for (i=0;i<this->maxlength;i++) { dataTemp[i]=this->data[i]; } this->maxlength=this->maxlength*2; for (i=0;i<this->maxlength;i++) { dataTemp[i]=NULL; } SafeDeleteArray(this->data); this->data=dataTemp; } this->data[this->length]=element; this->length++; }; //清空vector void clear(){ SafeDeleteArray(this->data); this->length=0; this->maxlength=10; this->data=new T[this->maxlength]; for (unsigned i=0;i<this->maxlength;i++) { this->data[i]=NULL; } }; //获得下标为i的对象 T get(unsigned i){ if (i>=this->length) { return NULL; } return this->data[i]; }; //设置下标为i的对象,返回原来的对象 T set(unsigned i, T element){ if (i>=this->maxlength) { unsigned j; T* dataTemp=new T[2*i]; for (j=0;j<this->maxlength;j++) { dataTemp[j]=this->data[j]; } this->maxlength=2*i; for (;j<this->maxlength;j++) { dataTemp[j]=NULL; } SafeDeleteArray(this->data); this->data=dataTemp; } T olddata=this->data[i]; this->data[i]=element; if (this->length<i+1) { this->length=i+1; } return olddata; }; //获得vector元素个数 unsigned size(){ return this->length; }; //重载[]操作符 T& operator[](unsigned i){ if (i>=this->length) { printf("error: vector is out of vector.\n"); exit(1); } return this->data[i]; }; //重载=操作符 MyVector& operator=(const MyVector & v){ this->length=v.length; this->maxlength=v.maxlength; SafeDeleteArray(this->data); this->data=new T[this->maxlength]; for (unsigned i=0;i<this->maxlength;i++) { this->data[i]=v.data[i]; } return *this; }; private: T* data; //存储数据的数组 unsigned length; //数组元素 unsigned maxlength; //最大可存储空间 }; #endif
main.cpp:
#include <stdio.h> #include <stdlib.h> #include "MyVector.h" int main(){ MyVector<int> v1; v1.insert(3); MyVector<int> v2; v2=v1; printf("%d\n",v1[0]); printf("%d\n",v2[0]); system("PAUSE"); return 0; }
注意到模板类的写法,最好将类的成员函数在类声明时定义。否则可能会遇到一些编译错误。具体看http://blog.csdn.net/jfkidear/article/details/7888167
以上的MyVector是一个简单版本的。需要说明的一点是,若MyVector<int> v2=v1,调用的是拷贝构造函数。若MyVector<int> v2; v2=v1; 则调用的是=重载函数。因此,=操作符必须重写,拷贝构造函数要重写。
转自:康瑞的部落 ? C++自定义Vector模板