[C++] stash的struct类型相关代码 →→→→→进入此内容的聊天室

来自 , 2019-06-06, 写在 C++, 查看 127 次.
URL http://www.code666.cn/view/56f9f889
  1. //stash.h文件
  2. #include <iostream>
  3. #include <string>
  4. #include <assert.h>
  5. using namespace std;
  6.  
  7. struct Stash
  8. {
  9.  
  10.         int size;
  11.         int quantity;
  12.         int next;
  13.         unsigned char* storage;
  14.         void initialize(int Size);
  15.         void cleanup();
  16.         int add( void* element);
  17.         void* fetch(int index);
  18.         int count();
  19.         void inflate(int increase);
  20. };
  21. //stash.cpp文件
  22. #include "stash.h"
  23.  
  24. const int CREASE_SIZE = 100;
  25. Stash::Stash(int Size)
  26. {
  27.         size = Size;
  28.         quantity = 0;
  29.         storage = 0;
  30.         next = 0;
  31. }
  32. Stash::Stash(int Size,int InitQuant)
  33. {
  34.         size = Size;
  35.         quantity = 0;
  36.         next = 0;
  37.         storage = 0;
  38.         inflate(InitQuant);
  39. }
  40. void Stash::cleanup()
  41. {
  42.         if (storage)
  43.         {
  44.                 cout<<"freeing storage"<<endl;
  45.                 free(storage);
  46.         }
  47. }
  48.  
  49. int Stash::add(void* element)
  50. {
  51.         if (next >= quantity)
  52.         {
  53.                 inflate(CREASE_SIZE);
  54.         }
  55.         memcpy(&(storage[next*size]),element,size);
  56.         next++;
  57.         return next - 1;
  58. }
  59.  
  60. void* Stash::fetch(int index)
  61. {
  62.         if ((index >= next) || index < 0)
  63.         {
  64.                 return 0;
  65.         }
  66.         return &(storage[index*size]);
  67. }
  68. int Stash::count()
  69. {
  70.         return next;
  71. }
  72.  
  73. void Stash::inflate(int increase)
  74. {
  75.         void* v = realloc(storage,(quantity+increase)*size);
  76.         assert(v);
  77.         storage = (unsigned char*)v;
  78.         quantity += increase;
  79. }
  80. //stash的main()函数文件
  81. #include "stash.h"
  82. const int BUFSIZE = 80;
  83. int main(void)
  84. {
  85.         Stash intStash;
  86.         int i;
  87.         intStash.initialize(sizeof(int));
  88.         for (i=0; i<100; i++)
  89.         {
  90.                 intStash.add(&i);
  91.         }
  92.         for (i=0; i<intStash.count(); i++)
  93.         {
  94.                 cout<<*(int*)intStash.fetch(i)<<" ";
  95.         }
  96.         cout<<endl;
  97.         intStash.cleanup();
  98.        
  99.         //Stash stringStash;
  100.         //FILE* file;
  101.         //char buf[BUFSIZE];
  102.         //char* cp;
  103.         //stringStash.initialize(sizeof(char)*BUFSIZE);
  104.         //file = fopen("test.cpp","r");
  105.         //assert(file);
  106.         //while (fgets(buf,BUFSIZE,file))
  107.         //{
  108.         //      stringStash.add(buf);
  109.         //}
  110.         //fclose(file);
  111.         //
  112.         //i = 0;
  113.         //while (((cp = (char*)stringStash.fetch(i++)) != 0))
  114.         //{
  115.         //      cout<<cp<<" ";
  116.         //}
  117.         //cout<<endl;
  118.         //stringStash.cleanup();
  119.         return 0;
  120. }
  121.  

回复 "stash的struct类型相关代码"

这儿你可以回复上面这条便签

captcha