关键词搜索

源码搜索 ×
×

《C++编程思想》中的CStash第一个程序的欣赏

发布2014-11-02浏览8377次

详情内容

        看着《C++编程思想》, 有点意思羡慕, 来欣赏一下其中的CStash程序(有略微的改动):

 

  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5. // element: 存储元素
  6. // cell : 存储空间(除了element之外, 可能还有没有利用、但是已经存在的cell空间)
  7. const int increment = 100; // 定义每次膨胀的cell的个数
  8. typedef struct CStashTag
  9. {
  10. int size; //一个cell的字节数
  11. int quantity; //cell的总数
  12. int next; //element的总数(也可以认为是下一个element的位置)
  13. unsigned char* storage; // 指向cell堆空间
  14. }CStash;
  15. void initiallize(CStash* s, int size);
  16. void cleanup(CStash* s);
  17. int add(CStash* s, const void* element);
  18. void* fetch(CStash* s, int index);
  19. int count(CStash* s);
  20. void inflate(CStash* s, int increase);
  21. // 初始化后, 就知道每个cell的大小了, 若element为int, 则一个cell的大小为4
  22. void initialize(CStash* s, int sz)
  23. {
  24. s->size = sz;
  25. s->quantity = 0;
  26. s->storage = NULL;
  27. s->next = 0;
  28. }
  29. int add(CStash* s, const void* element)
  30. {
  31. if(s->next >= s->quantity) // cell数目不足以容纳更多的element, 则需要膨胀
  32. {
  33. inflate(s, increment); // 每次膨胀100个cell
  34. }
  35. // 增加一个element, 需要这个element中的值拷贝到下一个cell空间
  36. int startBytes = s->next * s->size;
  37. unsigned char* e = (unsigned char*)element;
  38. for(int i = 0; i < s->size; i++) //逐个字节拷贝
  39. {
  40. s->storage[startBytes + i] = e[i];
  41. }
  42. s->next++; // element数目自增
  43. return(s->next - 1); //最后一个element的下标
  44. }
  45. void* fetch(CStash* s,int index)
  46. {
  47. if (index >= s->next)
  48. {
  49. return NULL;
  50. }
  51. // 返回index所对应的地址
  52. return &(s->storage[index * s->size]);
  53. }
  54. int count(CStash* s)
  55. {
  56. return s->next; //element的个数(也可以认为是下一个element的位置)
  57. }
  58. void inflate(CStash* s, int increase)
  59. {
  60. int newQuantity = s->quantity + increase; // 膨胀后的cell的总数
  61. int newBytes = newQuantity * s->size; // 原来cell的总空间
  62. int oldBytes = s->quantity * s->size; // 膨胀后cell的总空间
  63. unsigned char* b = new unsigned char[newBytes];
  64. for(int i = 0; i < oldBytes; i++)
  65. {
  66. b[i] = s->storage[i]; //将旧内容复制到新的空间中, 也是逐字节复制
  67. }
  68. delete [](s->storage);//删除旧的存储空间
  69. s->storage = b; //指向新的堆空间
  70. s->quantity = newQuantity; // 膨胀后的cell的总数
  71. }
  72. void cleanup(CStash* s)
  73. {
  74. // 我有一点点纳闷, 为什么其他的参数不清零, 而只进行下面的堆空间释放呢?
  75. if(NULL != s->storage)
  76. {
  77. cout << "freeing storage" << endl;
  78. delete []s->storage;
  79. }
  80. }
  81. int main()
  82. {
  83. CStash intStash,stringStash;
  84. int i = 0;
  85. // 初始化int对应的cell
  86. initialize(&intStash, sizeof(int));
  87. // 申请cell, 并将element写入cell
  88. for(i = 0; i < 10; i++)
  89. {
  90. add(&intStash, &i);
  91. }
  92. // 打印cell空间中的element
  93. for(i = 0; i < count(&intStash); i++)
  94. {
  95. cout << *(int*)fetch(&intStash, i) << endl;
  96. }
  97. // 如下代码功能类似上面, 所以我就不注释了
  98. ifstream in;
  99. string line;
  100. const int bufsize = 80;
  101. in.open("main.cpp");
  102. initialize(&stringStash, sizeof(char) * bufsize);
  103. while(getline(in, line))
  104. {
  105. add(&stringStash, line.c_str());
  106. }
  107. i = 0;
  108. char* cp = NULL;
  109. while(NULL != (cp = (char*)fetch(&stringStash, i++)))
  110. {
  111. cout << cp << endl;
  112. }
  113. cleanup(&intStash);
  114. cleanup(&stringStash);
  115. return 0;
  116. }

       程序的结果为:

 

0
1
2
3
4
5
6
7
8
9
#include <fstream>
#include <iostream>
#include <string>
using namespace std;


// element: 存储元素
// cell   : 存储空间(除了element之外, 可能还有没有利用、但是已经存在的cell空间


const int increment = 100; // 定义每次膨胀的cell的个数


typedef struct CStashTag
{
    int size;       //一个cell的字节数
    int quantity;   //cell的总数
    int next;       //element的总数(也可以认为是下一个element的位置)
    unsigned char* storage; // 指向cell堆空间
}CStash;


void initiallize(CStash* s, int size);
void cleanup(CStash* s);
int add(CStash* s, const void* element);
void* fetch(CStash* s, int index);
int count(CStash* s);
void inflate(CStash* s, int increase);


// 初始化后, 就知道每个cell的大小了, 若element为int, 则一个cell的大小为4
void initialize(CStash* s, int sz)
{
    s->size = sz;
    s->quantity = 0;
    s->storage = NULL;
    s->next = 0;
}


int add(CStash* s, const void* element)
{
    if(s->next >= s->quantity) // cell数目不足以容纳更多的element, 则需要膨胀
        {
        inflate(s, increment); // 每次膨胀100个cell
        }


        // 增加一个element, 需要这个element中的值拷贝到下一个cell空间
    int startBytes = s->next * s->size;
    unsigned char* e = (unsigned char*)element;
    for(int i = 0; i < s->size; i++) //逐个字节拷贝
        {
        s->storage[startBytes + i] = e[i];
        }


    s->next++; // element数目自增


    return(s->next - 1); //最后一个element的下标
}


void* fetch(CStash* s,int index)
{
    if (index >= s->next)
        {
        return NULL;
        }


    // 返回index所对应的地址
    return &(s->storage[index * s->size]);
}


int count(CStash* s)
{
    return s->next; //element的个数(也可以认为是下一个element的位置)
}


void inflate(CStash* s, int increase)
{
    int newQuantity = s->quantity + increase; // 膨胀后的cell的总数
    int newBytes = newQuantity * s->size; // 原来cell的总空间
    int oldBytes = s->quantity * s->size; // 膨胀后cell的总空间


        unsigned char* b = new unsigned char[newBytes];
    for(int i = 0; i < oldBytes; i++)
        {
       b[i] = s->storage[i]; //将旧内容复制到新的空间中, 也是逐字节复制
        }


    delete [](s->storage);//删除旧的存储空间


        s->storage = b; //指向新的堆空间
    s->quantity = newQuantity; // 膨胀后的cell的总数
}


void cleanup(CStash* s)
{
        // 我有一点点纳闷, 为什么其他的参数不清零, 而只进行下面的堆空间释放呢?


    if(NULL != s->storage)
        {
        cout << "freeing storage" << endl;
        delete []s->storage;
    }
}




int main()
{


        CStash intStash,stringStash;
        int i = 0;


        // 初始化int对应的cell
        initialize(&intStash, sizeof(int));


        // 申请cell, 并将element写入cell
        for(i = 0; i < 10; i++)
        {
                add(&intStash, &i);
        }


        // 打印cell空间中的element
        for(i = 0; i < count(&intStash); i++)
        {
                cout << *(int*)fetch(&intStash, i) << endl;
        }




        // 如下代码功能类似上面, 所以我就不注释了




        ifstream in;
        string line;
        const int bufsize = 80;
        in.open("main.cpp");


        initialize(&stringStash, sizeof(char) * bufsize);
        while(getline(in, line))
        {
                add(&stringStash, line.c_str());
        }


        i = 0;
        char* cp = NULL;
        while(NULL != (cp = (char*)fetch(&stringStash, i++)))
        {
                cout << cp << endl;
        }


        cleanup(&intStash);
        cleanup(&stringStash);


        return 0;
}
freeing storage
freeing storage


       其实, 从上面可以看到, CStash结构体显得有点多余, 直接用size, quantity, storage, next也是可以的。 之所以还要用结构体, 完全是为了整体性, 让有点关系的size, quantity, storage, next成为一个有机体, 便于理解, 便于编程, 便于维护, 这就是结构体的最大作用吧微笑

相关技术文章

最新源码

下载排行榜

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载