zoukankan      html  css  js  c++  java
  • 【ThinkingInC++】66、pointer Stash的使用

    头文件PStash.h


    /**
    * 书本:【ThinkingInC++】
    * 功能:pointer Stash的头文件
    * 时间:2014年10月5日14:33:15
    * 作者:cutter_point
    */
    #ifndef PSTASH_H_INCLUDED
    #define PSTASH_H_INCLUDED
    
    class 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;
    }
    
    






  • 相关阅读:
    Converting PDF to Text in C#
    Working with PDF files in C# using PdfBox and IKVM
    Visualize Code with Visual Studio
    Azure Machine Learning
    Building Forms with PowerShell – Part 1 (The Form)
    ML.NET is an open source and cross-platform machine learning framework
    Microsoft Visual Studio Tools for AI
    Debugging Beyond Visual Studio – WinDbg
    Platform.Uno介绍
    Hawk-数据抓取工具
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/4297464.html
Copyright © 2011-2022 走看看