- 论坛徽章:
- 0
|
It is really a stupid idea. C没有办法对[]进行重载,光这一点就不能叫vector。倘若朋友们也无聊的话,一起看看吧。
vector.h文件:
- typedef int ElementType;
- #ifndef _VECTOR_H
- #define _VECTOR_H
- typedef struct vector{
- ElementType *array;
- int size;
- int resv;
- } vector;
- typedef vector *vectorp;
- void Initialize(vectorp vp);
- int IsEmpty(vectorp vp);
- int Size(vectorp vp);
- int Capacity(vectorp vp);
- void Reserve(vectorp vp,int n);
- ElementType At(vectorp vp,int i);
- void Assign(vectorp vp,int i,ElementType elem);
- void Swap(vectorp vp,int i,int j);
- ElementType Front(vectorp vp);
- ElementType Back(vectorp vp);
- void Clear(vectorp vp);
- void PushBack(vectorp vp,ElementType elem);
- void PopBack(vectorp vp);
- void Delete(vectorp vp);
- #endif
复制代码
vector.c文件:
- #include <stdio.h>;
- #include <stdlib.h>;
- #include <assert.h>;
- #include <string.h>;
- #include "vector.h"
- void Initialize(vectorp vp)
- {
- vp->;array = NULL;
- vp->;size = 0;
- vp->;resv = 0;
- }
- int IsEmpty(vectorp vp)
- {
- return vp->;size == 0 ;
- }
- int Size(vectorp vp)
- {
- return vp->;size;
- }
- int Capacity(vectorp vp)
- {
- return vp->;resv;
- }
- void Reserve(vectorp vp,int n)
- {
- if(n <= vp->;resv)
- return;
- else
- {
- if(vp->;resv == 0)
- vp->;array = NULL;
- if( (vp->;array = realloc
- (vp->;array,sizeof(ElementType)*n) ) == NULL)
- {
- printf("Out of space!!\n");
- exit(1);
- }
- vp->;resv = n;
- }
- }
- ElementType At(vectorp vp,int i)
- {
- if((i < 0)||(i >;= vp->;size))
- {
- printf("WARNING:At() be invoked to return an element OUT OF RANGE...\n");
- return 0;//to avoid warning
- }
- return vp->;array[i];
- }
- void Assign(vectorp vp,int i,ElementType elem)
- {
- if((i < 0)||(i >;= vp->;size))
- {
- printf("WARNING:Assign() be invoked to assign an element OUT OF RANGE...\n");
- return;
- }
- vp->;array[i] = elem;
- }
- void Swap(vectorp vp,int i,int j)
- {
- if( (i<0)||(j<0)||(i>;=vp->;size)||(j>;=vp->;size) )
- {
- printf("Swap() be invoked to access elements OUT OF RANGE...\n");
- return;
- }
- if(i == j)
- return;
- ElementType tmp = vp->;array[i];
- vp->;array[i] = vp->;array[j];
- vp->;array[j] = tmp;
- }
- ElementType Front(vectorp vp)
- {
- assert(!IsEmpty(vp));
- return vp->;array[0];
- }
- ElementType Back(vectorp vp)
- {
- assert(!IsEmpty(vp));
- return vp->;array[vp->;size - 1];
- }
- void Clear(vectorp vp)
- {
- realloc(vp->;array,0);
- Initialize(vp);
- }
- void PushBack(vectorp vp,ElementType elem)
- {
- if(vp->;size == vp->;resv)
- Reserve(vp,vp->;resv + 10);
- vp->;array[vp->;size] = elem;
- vp->;size += 1;
- }
- void PopBack(vectorp vp)
- {
- assert(!IsEmpty(vp));
- bzero( (ElementType *)(&(vp->;array[vp->;size - 1])),
- sizeof(ElementType) );
- vp->;size --;
- }
- void Delete(vectorp vp)
- {
- realloc(vp->;array,0);
- Initialize(vp);
-
- }
复制代码
main.c文件,验证一下使用有没有问题:
- #include <stdio.h>;
- #include <stdlib.h>;
- #include <string.h>;
- #include <assert.h>;
- #include "vector.h"
- int main(int argc,char** argv)
- {
- int s1,s2;
- s1 = 11;
- s2 = 22;
- vector vc;
- Initialize(&vc);
- Reserve(&vc,10);
- int i ;
- for(i = 0; i < 10; i++)
- PushBack(&vc,s1);
- for(i = 0; i < 10; i++)
- {
- printf("vc[%d] is %d\n",i,At(&vc,i));
- }
- printf("\n");
- Assign(&vc,2,s2);
- printf("after assign vc[2]:\n");
- printf("vc[2] is %d\n",At(&vc,2));
- printf("\n");
- Swap(&vc,1,2);
- printf("after swap vc[1] and vc[2]:\n");
- printf("vc[2] is %d\n",At(&vc,2));
- printf("size of vc is %d\n",Size(&vc));
- PopBack(&vc);
- PopBack(&vc);
- printf("after popback twice,size of vc is %d\n",Size(&vc));
- Delete(&vc);
- return 0;
- }
复制代码 |
|