Stash4.h
/**
* 书本:【ThinkingInC++】
* 功能:带内联函数的Stash
* 时间:2014年9月12日08:16:13
* 作者:cutter_point
*/
#ifndef STASH4_H_INCLUDED
#define STASH4_H_INCLUDED
#include "../require.h"
#include <iostream>
class Stash
{
int size; //每个空间存储块的字节长度
int quantity; //内部定义的数据类型的存储块的个数
int next; //下一个空的内存块的索引
unsigned char* storage; //存储空间的大小
void inflate(int increase); //空间不够用的时候增加空间大小
public:
//构造函数,使用内联函数提高代码执行效率
Stash(int sz) : size(sz), quantity(0), next(0), storage(0) {}
Stash(int sz, int initQuantity) : size(sz), quantity(0), next(0), storage(0){inflate(initQuantity);}
~Stash() //回收内存空间
{
if(storage != 0)
delete [] storage;
std::cout<<"~Stash() 回收内存空间"<<std::endl;
}
int add(void* element); //添加元素
void* fetch(int index) const
{
require(0 <= index, "Stash::fetch (-)index");
if(index >= next)
return 0; //超出内存空间的大小
return &(storage[index*size]);
}
int count() const {return next;}
};
#endif // STASH4_H_INCLUDED
Stash4.cpp
/**
* 书本:【ThinkingInC++】
* 功能:带内联函数的Stash
* 时间:2014年9月12日08:17:14
* 作者:cutter_point
*/
#include "Stash4.h"
#include <cassert>
#include <iostream>
using namespace std;
const int increment=100;
// void inflate(int increase); //空间不够用的时候增加空间大小
// int add(void* element); //添加元素
void Stash::inflate(int increase)//空间不够用的时候增加空间大小
{
//判定increase是大于0的
assert(increase >= 0);
//判断是否是等于0,如果等于0那就直接返回不用增加
if(increase == 0) return;
//大于0 的话
//1、求得新空间的总个数
int newQuantity=quantity+increase;
//2、新空间需求的字节数
int newBytes=newQuantity*size;
//3、申请新空间需求的内存
unsigned char* b=new unsigned char[newBytes];
//4、吧旧的空间的数据转化到新空间上
//首先要只要旧的成员数据直接数
int oldBytes=quantity*size;
//移动到新的上
for(int i=0 ; i<oldBytes ; ++i)
b[i]=storage[i];
//5、内存回收
delete [](storage);
//6、新空间的成员数据更新
storage=b;
quantity=newQuantity;
}
// int add(void* element); //添加元素,并返回最后一个元素的位置
int Stash::add(void* element)
{
//判断能不能添加,空间够不够
if(next >= quantity)
inflate(increment);
//找到要添加的开始位置
int startBytes=next*size;
//吧element转化成unsigned char*的数组
unsigned char* e=(unsigned char*)element;
//添加元素到数据类型中
for(int i=0 ; i<size ; ++i) //一个元素的字节长度,就是添加一个元素的长度
storage[startBytes+i]=e[i];
//吧next更新
++next;
//返回位置
return (next-1);
}
Stash4Test.cpp
/**
* 书本:【ThinkingInC++】
* 功能:带内联函数的Stash
* 时间:2014年9月12日08:18:19
* 作者:cutter_point
*/
#include "Stash4.cpp"
#include "../require.h"
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
Stash intStash(sizeof(int)); //就是转化成int类型
for(int i=0 ; i<100 ; ++i) //吧0到99存入到这个数据类型的数组中
intStash.add(&i);
//全部输出看看
for(int j=0 ; j<100 ; ++j)
cout<<"intStash.fetch("<<j<<")="
<<*(int*)intStash.fetch(j)<<endl;
//再创建一个长度为80的字符串数组,每个字符串长度都是80个字节
const int bufsize=80;
Stash stringStash(sizeof(char)*bufsize, 100);
ifstream in("Stash4Test.cpp");
assure(in, "Stash4Test.cpp");
string line;
while(getline(in, line))
{
stringStash.add((char*)line.c_str()); //吧字符串转化成char*存入到数组中
}
//输出来看看
int k=0; //计数
char* cp; //指向每一个string的地址
while((cp=(char*)stringStash.fetch(k++)) != 0)
cout<<"stringStash.fetch("<<k<<")="<<cp<<endl;
return 0;
}
最后我们看一看关于枚举类型的知识
枚举类型直接输出里面的字符
http://topic.csdn.net/t/20050408/19/3921153.html
#include <iostream>
#include <string>
#include <map>
using namespace std;
enum MPType
{
MPT_None,
MPT_Other,
MPT_Board,
MPT_Length
};
//方案一,直接用数组
string MPTypeString[MPT_Length] = {
"MPT_None ",
"MPT_Other ",
"MPT_Board "
};
//方案二,用map
class MPTypeConverter {
public:
MPTypeConverter() {
map.insert(make_pair(MPT_None,"MPT_None "));
map.insert(make_pair(MPT_Other,"MPT_Other "));
map.insert(make_pair(MPT_Board,"MPT_Board "));
}
string ToString(MPType key) {
MPTypeStringMap::iterator pos =map.find(key);
if (pos != map.end())
return pos-> second;
return string( " ");
}
private:
typedef map <MPType, string>MPTypeStringMap;
MPTypeStringMap map;
};
int main()
{
MPTypeConverter converter;
cout < < MPTypeString[MPT_Board]< < endl;
cout < < converter.ToString(MPT_Board)< < endl;
return 0;
}