【ThinkingInC++】24、基本对象,用struct写一个类

/**
* 功能:基本对象,用struct写一个类
* 时间:2014年8月17日08:08:47
* 作者:cutter_point
*/
#ifndef CPPLIB_H_INCLUDED
#define CPPLIB_H_INCLUDED

struct Stash
{
    int size;       //每个小空间的大小,就是确定存储什么元素的时候一个元素的大小,字节数
    int quantity;   //用来表示要分配多少个元素个数,全部空间大小
    int next;       //已经存放了数据的元素个数
    //首先,我们不知道要分配的类型大小是什么,所以用最小的单位作为空间大小
    unsigned char* storage;

    void initialize(int size);   //初始化空间大小,每个元素空间占用的字节数
    void cleanup();    //回收内存
    int add(const void* element);    //添加元素
    void* fetch(int index); //返回index对应的元素值
    int count();    //已经存放了数据的元素个数
    void inflate(int increase); //添加空间
};


#endif // CPPLIB_H_INCLUDED


/**
* 功能:基本对象,用struct写一个类
* 时间:2014年8月17日08:08:53
* 作者:cutter_point
*/

#include"CppLib.h"
#include<iostream>
#include<cassert>

using namespace std;

//定义全局变量,添加空间的基本数据
const int increment=100;

void Stash::initialize(int sz)
{
    size=sz;
    quantity=0;
    storage=0;
    next=0;
}

void Stash::cleanup()
{
    if(storage != 0)
    {
        cout<<"free storage"<<endl;
        delete [] storage;
    }
}

//int add(const void* element);    //添加元素
int Stash::add(const void* element)
{
    if(next >= quantity)    //next已经存放了数据的元素个数,quantity用来表示要分配多少个字节,全部空间大小
        inflate(increment); //添加内存空间

    int startBytes=next*size;
    unsigned char* e=(unsigned char*)element;
    for(int i=0 ; i<size ; ++i)
        storage[startBytes+i]=e[i];

    next++;
    return next-1;
}

//void* fetch(int index);返回index对应的元素值
void* Stash::fetch(int index)
{
    assert(0 <= index);

    if(index >= next)
        return 0;

    return &(storage[index*size]);
}

//int count();    已经存放了数据的元素个数
int Stash::count()
{
    return next;
}

//    void inflate(int increase); //添加空间
void Stash::inflate(int increase)
{
    assert(increase>0);

    int newQuantity=quantity+increase;  //单位个数
    int newBytes=newQuantity*size;
    int oldBytes=quantity*size;
    unsigned char* b=new unsigned char[newBytes];

    for(int i=0 ; i<oldBytes ; ++i) //吧老的数据转移到新的空间上
        b[i]=storage[i];

    //吧旧的空间去除
    delete [] storage;
    storage=b;
    quantity=newQuantity;
}



/**
* 功能:基本对象,用struct写一个类,测试使用这个类
* 时间:2014年8月17日08:09:16
* 作者:cutter_point
*/

#include"CppLib.h"
#include"CppLib.cpp"
#include<cassert>
#include<fstream>
#include<iostream>
#include<string>

using namespace std;

int main()
{
    //给Stash添加100个int类型的元素,然后输出他们
    Stash intStash;
    intStash.initialize(sizeof(int));//初始化空间大小,每个元素空间占用的字节数
    //添加100个int元素
    for(int i=0 ; i<100 ; ++i)
        intStash.add(&i);
    //输出这100个元素
    for(int j=0 ; j<intStash.count() ; ++j)
        cout<<"fetch("<<j<<")="<<*(int*)intStash.fetch(j)<<endl;

    cout<<"---------------------------------------"<<endl;

    //把这个cpp文件输出出来
    Stash stringStash;
    const int bufsize=80;   //表示每个string含有的char个数
    stringStash.initialize(sizeof(char)*bufsize);//初始化空间大小,每个元素空间占用的字节数
    ifstream in("CppLibTest.cpp");
    assert(in);
    string line;
    //取出每行string每行存入line
    while(getline(in, line))
        stringStash.add(line.c_str());//line.c_str()是一个指向line字符的指针,返回一个指向string的char指针
    int k=0;
    char* cp;
    while((cp=(char*)stringStash.fetch(k++)) != 0)
        cout<<"stringStash.fetch("<<k<<")="<<cp<<endl;

    intStash.cleanup();
    stringStash.cleanup();

    return 0;
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值