读书人

动态数组对照STL vector及其实现(c/c+

发布时间: 2012-11-26 11:48:49 作者: rapoo

动态数组对比STL vector及其实现(c/c++)

#define ARRAY_SIZE  1024#define ARRAY_NUMBER 1024typedef  int  Type;typedef struct dArray{    Type elementArray[ARRAY_SIZE];}DArray;DArray *pDynamicArray[ARRAY_NUMBER];int gAllocArraySize = 0;int gCurrentArrayIndex = 0;bool rellocArray(int size);bool insert_element(Type *value){    if(false ==rellocArray(sizeof(Type)))       return false;    if(gCurrentArrayIndex>gAllocArraySize-1)        return false;    int mainIndex = gCurrentArrayIndex / ARRAY_SIZE;    int elementArrayInex = (++gCurrentArrayIndex)%ARRAY_SIZE - 1;    pDynamicArray[mainIndex]->elementArray[elementArrayInex] = *value;        return true;}Type *get_element(int index){    if(index>=0 && index<gAllocArraySize)    {        int mainIndex = index/ARRAY_SIZE;        int elementArrayIndex = index%ARRAY_SIZE  - 1;        return &(pDynamicArray[mainIndex]->elementArray[elementArrayIndex]);    }    return NULL;}bool rellocArray(int size){    if(size> ARRAY_NUMBER*ARRAY_SIZE - gAllocArraySize)        return false;    while(gAllocArraySize<=size)    {        int mainIndex =  gAllocArraySize /ARRAY_SIZE;        pDynamicArray[mainIndex] = (DArray*)calloc(1,sizeof(DArray));        if(NULL==pDynamicArray[mainIndex])            return false;        gAllocArraySize += ARRAY_SIZE;    }    return true;}


转转请注明:点击打开链接http://blog.csdn.net/typename/article/details/8202741





读书人网 >C++

热点推荐