提示:以下代码均未经详细测试。如果哪位有空,提出点意见,不胜感激,另本主题是用宏实现,所以不相关的就不要多说了。
#ifndef _VECTOR_H_
#define _VECTOR_H_
#include
typedef struct _vector{
int size;
void *data;
}VECTOR,*PVECTOR;
#define vector_init(x,t,z) do{/
PVECTOR v;int n;/
v=(PVECTOR)(x);n=(int)(z);/
v->data=malloc(v->size=sizeof(t)*n);/
}while(0)
#define vector_cleanup(x) free((x)->data)
#define vector_get(v,t,n) (((t*)((v)->data))[n])
#define vector_set_fast(v,t,n,d) (((t*)((v)->data))[n]=(d))
#define vector_set(x,t,z,d) do{/
PVECTOR v;int n;t temp;/
v=(PVECTOR)(x);n=(int)(z);temp=(d)/
if(n*sizeof(t)>v->size)/
{/
void *dt;
dt=realloc(v->size=n*sizeof(t));/
if(!dt) break;/
v->data=dt;/
}/
memcpy(((t*)v->data)+n,&temp,sizeof(t));/
}while(0)
#endif