【ThinkingInC++】66、pointer Stash的使用

来源:互联网 发布:matlab 2015a mac 编辑:程序博客网 时间:2024/06/06 01:32

头文件PStash.h


/*** 书本:【ThinkingInC++】* 功能:pointer Stash的头文件* 时间:2014年10月5日14:33:15* 作者:cutter_point*/#ifndef PSTASH_H_INCLUDED#define PSTASH_H_INCLUDEDclass PStash{    int quantity;   //内部定义的数据类型的存储块的个数    int next;       //下一个空的空间的位置    void** storage;     //指向一个指向void*的指针    void inflate(int increase); //增加内存空间public:    //构造函数    PStash() : quantity(0), storage(0), next(0) {}    ~PStash();  //析构函数    int add(void* element); //添加元素    void* operator [] (int index) const;    //运算符重载    void* remove(int index);        //移除index索引下的元素    int count() const {return next;}    //返回一共有多少个元素};#endif // PSTASH_H_INCLUDED

定义文件PStash.cpp

/*** 书本:【ThinkingInC++】* 功能:pointer Stash的定义文件* 时间:2014年10月5日14:33:49* 作者:cutter_point*/#include "PStash.h"#include "../require.h"#include <iostream>#include <cstring>using namespace std;/*    int quantity;   //内部定义的数据类型的存储块的个数    int next;       //下一个空的空间的位置    void** storage;     //指向一个指向void*的指针    void inflate(int increase); //增加内存空间public:    //构造函数    PStash() : quantity(0), storage(0), next(0) {}    ~PStash();  //析构函数    int add(void* element); //添加元素    void* operator [] (int index) const;    //运算符重载    void* remove(int index);        //移除index索引下的元素    int count() const {return next;}    //返回一共有多少个元素*/void PStash::inflate(int increase)  //增加内存空间{    const int psz=sizeof(void*);    //求出每块最小存储单元的长度    void** st=new void*[quantity+increase]; //增加的空间    //吧新的空间初始化    memset(st, 0, (quantity+increase)*psz);    //吧旧空间的内容拷贝到新空间    memcpy(st, storage, quantity*psz);    //吧数据刷新    quantity+=increase;    //回收相应的空间    delete []storage;    //刷新数据    storage=st;}//    ~PStash();  //析构函数PStash::~PStash(){    for(int i=0 ; i<next ; ++i)        require(storage[i] == 0, "PStash not cleaned up");    delete []storage;}//int add(void* element); //添加元素int PStash::add(void* element){//添加元素    //判断给定的空间是否够,不够那就增加    const int inflateSize=10;   //用来增加长度    if(next >= quantity)        inflate(inflateSize);    //空间够了,那么就吧元素输入到数组里面去    storage[next++]=element;    return (next-1);    //吧添加进去的的索引返回}//    void* operator [] (int index) const;    //运算符重载void* PStash::operator [] (int index) const{    //要检验给的index是否合理    require(index >= 0, "PStash::operator [] index negative");    //既然数据合理,判断数据是否超出了界限    if(index >= next)        return 0;    //返回相应的索引的数据    return storage[index];}//    void* remove(int index);        //移除index索引下的元素void* PStash::remove(int index){    void* v=operator[](index);    //移除指针    if(v != 0)        storage[index]=0;   //这里吧指针置为0之后,但是没有吧内存的位置改变,下一个加入的内存开始还是next    return v;}

最终的测试文件PStashTest.cpp


/*** 书本:【ThinkingInC++】* 功能:pointer Stash的测试文件* 时间:2014年10月5日14:34:23* 作者:cutter_point*/#include "PStash.cpp"#include "../require.h"#include <iostream>#include <fstream>#include <string>using namespace std;int main(){    PStash intStash;    for(int i=0 ; i<25 ; ++i)        intStash.add(new int(i));    //输出元素内容    for(int i=0 ; i<intStash.count() ; ++i)    {        cout<<"intStash["<<i<<"] = "<<*(int*)intStash[i]<<endl;    }    //清除,回收内存    for(int i=0 ; i<intStash.count() ; ++i)        delete intStash.remove(i);    //输出当前文件    ifstream in("PStashTest.cpp");    assure(in, "PStashTest.cpp");    PStash stringStash;    string line;    while(getline(in, line))    {        stringStash.add(new string(line));    }    //输出字符串    for(int u=0 ; stringStash[u] ; ++u)        cout<<"stringStash["<<u<<"] ="<<*(string*)stringStash[u]<<endl;    //清除内存    for(int v=0 ; v<stringStash.count() ; ++v)        delete (string*)stringStash.remove(v);    return 0;}






0 0