免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4988 | 回复: 8
打印 上一主题 下一主题

闲着无聊,实现个C的vector [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-02-02 09:46 |只看该作者 |倒序浏览
It is really a stupid idea. C没有办法对[]进行重载,光这一点就不能叫vector。倘若朋友们也无聊的话,一起看看吧。

vector.h文件:
  1. typedef int ElementType;

  2. #ifndef _VECTOR_H
  3. #define _VECTOR_H

  4. typedef struct vector{
  5.         ElementType *array;
  6.         int size;
  7.         int resv;
  8. } vector;

  9. typedef vector *vectorp;


  10. void Initialize(vectorp vp);

  11. int IsEmpty(vectorp vp);

  12. int Size(vectorp vp);

  13. int Capacity(vectorp vp);

  14. void Reserve(vectorp vp,int n);

  15. ElementType At(vectorp vp,int i);

  16. void Assign(vectorp vp,int i,ElementType elem);

  17. void Swap(vectorp vp,int i,int j);

  18. ElementType Front(vectorp vp);

  19. ElementType Back(vectorp vp);

  20. void Clear(vectorp vp);

  21. void PushBack(vectorp vp,ElementType elem);

  22. void PopBack(vectorp vp);

  23. void Delete(vectorp vp);

  24. #endif
复制代码


vector.c文件:
  1. #include <stdio.h>;
  2. #include <stdlib.h>;
  3. #include <assert.h>;
  4. #include <string.h>;

  5. #include "vector.h"



  6. void Initialize(vectorp vp)
  7. {
  8.         vp->;array = NULL;
  9.         vp->;size = 0;
  10.         vp->;resv = 0;
  11. }

  12. int IsEmpty(vectorp vp)
  13. {
  14.         return vp->;size == 0 ;
  15. }

  16. int Size(vectorp vp)
  17. {
  18.         return vp->;size;
  19. }

  20. int Capacity(vectorp vp)
  21. {
  22.         return vp->;resv;
  23. }

  24. void Reserve(vectorp vp,int n)
  25. {
  26.         if(n <= vp->;resv)
  27.                 return;
  28.         else
  29.         {
  30.                 if(vp->;resv == 0)
  31.                         vp->;array = NULL;
  32.                 if( (vp->;array = realloc
  33.                         (vp->;array,sizeof(ElementType)*n) ) == NULL)
  34.                 {
  35.                         printf("Out of space!!\n");
  36.                         exit(1);
  37.                 }
  38.                 vp->;resv = n;
  39.         }
  40. }

  41. ElementType At(vectorp vp,int i)
  42. {
  43.         if((i < 0)||(i >;= vp->;size))
  44.         {
  45.                 printf("WARNING:At() be invoked to return an element OUT OF RANGE...\n");
  46.                 return 0;//to avoid warning
  47.         }

  48.         return vp->;array[i];
  49. }

  50. void Assign(vectorp vp,int i,ElementType elem)
  51. {
  52.         if((i < 0)||(i >;= vp->;size))
  53.         {
  54.                 printf("WARNING:Assign() be invoked to assign an element OUT OF RANGE...\n");
  55.                 return;
  56.         }

  57.         vp->;array[i] = elem;
  58. }

  59. void Swap(vectorp vp,int i,int j)
  60. {
  61.         if( (i<0)||(j<0)||(i>;=vp->;size)||(j>;=vp->;size) )
  62.         {
  63.                 printf("Swap() be invoked to access elements OUT OF RANGE...\n");
  64.                 return;
  65.         }

  66.         if(i == j)
  67.                 return;
  68.         ElementType tmp = vp->;array[i];
  69.         vp->;array[i] = vp->;array[j];
  70.         vp->;array[j] = tmp;
  71. }

  72. ElementType Front(vectorp vp)
  73. {
  74.         assert(!IsEmpty(vp));
  75.         return vp->;array[0];
  76. }

  77. ElementType Back(vectorp vp)
  78. {
  79.         assert(!IsEmpty(vp));
  80.         return vp->;array[vp->;size - 1];
  81. }

  82. void Clear(vectorp vp)
  83. {
  84.         realloc(vp->;array,0);
  85.         Initialize(vp);
  86. }

  87. void PushBack(vectorp vp,ElementType elem)
  88. {
  89.         if(vp->;size == vp->;resv)
  90.                 Reserve(vp,vp->;resv + 10);

  91.         vp->;array[vp->;size] = elem;
  92.         vp->;size += 1;
  93. }

  94. void PopBack(vectorp vp)
  95. {
  96.         assert(!IsEmpty(vp));
  97.         bzero( (ElementType *)(&(vp->;array[vp->;size - 1])),
  98.                                 sizeof(ElementType) );
  99.         vp->;size --;

  100. }

  101. void Delete(vectorp vp)
  102. {
  103.         realloc(vp->;array,0);
  104.         Initialize(vp);
  105.         
  106. }
复制代码


main.c文件,验证一下使用有没有问题:
  1. #include <stdio.h>;
  2. #include <stdlib.h>;
  3. #include <string.h>;
  4. #include <assert.h>;

  5. #include "vector.h"

  6. int main(int argc,char** argv)
  7. {
  8.         int s1,s2;
  9.         s1 = 11;
  10.         s2 = 22;

  11.         vector vc;
  12.         Initialize(&vc);

  13.         Reserve(&vc,10);
  14.         int i ;
  15.         for(i = 0; i < 10; i++)
  16.                 PushBack(&vc,s1);
  17.         for(i = 0; i < 10; i++)
  18.         {
  19.                 printf("vc[%d] is %d\n",i,At(&vc,i));
  20.         }

  21.         printf("\n");

  22.         Assign(&vc,2,s2);
  23.         printf("after assign vc[2]:\n");
  24.         printf("vc[2] is %d\n",At(&vc,2));

  25.         printf("\n");
  26.         Swap(&vc,1,2);
  27.         printf("after swap vc[1] and vc[2]:\n");
  28.         printf("vc[2] is %d\n",At(&vc,2));

  29.         printf("size of vc is %d\n",Size(&vc));
  30.         PopBack(&vc);
  31.         PopBack(&vc);
  32.         printf("after popback twice,size of vc is %d\n",Size(&vc));



  33.         Delete(&vc);
  34.         return 0;
  35. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2005-02-02 09:52 |只看该作者

闲着无聊,实现个C的vector


  1. typedef struct vector{
  2.         ElementType *array;
  3.         int size;
  4.         int resv;
  5. } vector;

  6. typedef vector *vectorp;
复制代码


直接这样就可以了:

  1. typedef struct vector{
  2.         ElementType *array;
  3.         int size;
  4.         int resv;
  5. } vector, *vectorp;
复制代码

论坛徽章:
0
3 [报告]
发表于 2005-02-02 10:01 |只看该作者

闲着无聊,实现个C的vector

是啊。原来我在vector.h中少写了一个分号,编译时出现下列错误:
gcc -Wall -g main.c vector.c
main.c: In function `Swap':
main.c:9: warning: `main' is usually a function
等等。。。


我担心是类型定义的问题,又担心是不是哪里用错输入法了,要不然怎么编译器老说胡话!!狂找两天,才发现少了个分号

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
4 [报告]
发表于 2005-02-02 10:07 |只看该作者

闲着无聊,实现个C的vector

1、Reserve函数中realloc之后不对vp->;resv 赋值可能会内存泄漏吧?
2、Clear函数中直接Initialize,不先做个free?
3、Delete函数也是同样
4、PopBack最好把最后一个值返回出来吧?

论坛徽章:
0
5 [报告]
发表于 2005-02-02 10:09 |只看该作者

闲着无聊,实现个C的vector

给你一个建议,这是我得血和泪得经验教训得来的:
写代码的时候,写一个函数就进行测试,不要写了一大段才来一个main函数测试,可能其中出现了问题无从查找.如果要做到写一个函数就测试一个的话,那么就要确定函数之间的依赖的关系,在依赖关系中最底层的最早测试,确认无误再测试上面调用了这个函数的函数...依此类推.

我曾经一下写了1000行左右的程序,最后出错无从查找,惨就一个字.

论坛徽章:
0
6 [报告]
发表于 2005-02-02 10:28 |只看该作者

闲着无聊,实现个C的vector

原帖由 "yuxh" 发表:
1、Reserve函数中realloc之后不对vp->;resv 赋值可能会内存泄漏吧?
2、Clear函数中直接Initialize,不先做个free?
3、Delete函数也是同样
4、PopBack最好把最后一个值返回出来吧?


您真是眼光犀利!!2,3谨按所指正改过。谢谢
第一条为什么要对vp->;resv赋值呢?还请赐教
第四条试了STL的vector,也是void的,因为提供了Front和Back函数,就没有再用popback返回元素了。

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
7 [报告]
发表于 2005-02-02 10:33 |只看该作者

闲着无聊,实现个C的vector

调两次Reserve就有问题
第2次vp->;array = NULL; 赋值了
还有,没看到你什么时候给vp->;resv赋值呀?它的值一直=0?

论坛徽章:
0
8 [报告]
发表于 2005-02-02 10:50 |只看该作者

闲着无聊,实现个C的vector

原帖由 "yuxh" 发表:
调两次Reserve就有问题
第2次vp->;array = NULL; 赋值了
还有,没看到你什么时候给vp->;resv赋值呀?它的值一直=0?


vp->;resv竟然忘了,惶窘啊!
那个vp->;array = NULL,我是这么想的:如果resv为0,就让vp->;array = NULL,这样调用realloc时就相当于调用malloc;如果resv不为0,那就说明已经realloc过至少一次了,此时再realloc就没问题。

您看这样行吗?

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
9 [报告]
发表于 2005-02-02 10:54 |只看该作者

闲着无聊,实现个C的vector

只要给vp->;resv赋值了就可以,我看了有些开源代码里就是差不多这样做的
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP