博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
袖珍C库
阅读量:4311 次
发布时间:2019-06-06

本文共 1994 字,大约阅读时间需要 6 分钟。

1 #include "StdAfx.h" 2 #include 
3 #include
4 #include
5 #include
6 7 using namespace std; 8 9 const int increment = 100;10 11 typedef struct CStashTag{12 int size;13 int quantity;14 int next;15 unsigned char* storage;16 }CStash;17 18 void initialize(CStash* s,int size);19 void cleanup(CStash* s);20 int add(CStash* s,const void* element);21 void* fetch(CStash* s,int index);22 int count(CStash* s);23 void inflate(CStash* s,int increase);24 25 void initialize(CStash* s,int sz){26 s->size = sz;27 s->quantity = 0;28 s->storage = 0;29 s->next = 0;30 }31 32 int add(CStash* s,const void* element){33 if(s->next >= s->quantity)34 inflate(s,increment);35 int startBytes = s->next * s->size;36 unsigned char* e=(unsigned char*)element;37 for(int i=0;i
size;i++)38 s->storage[startBytes + i] = e[i];39 s->next++;40 return(s->next -1);41 }42 43 void* fetch(CStash* s,int index){44 assert(0 <= index);45 if(index >= s->next)46 return 0;47 return &(s->storage[index * s->size]);48 }49 50 int count(CStash* s){51 return s->next;52 }53 54 void inflate(CStash* s,int increase){55 assert(increase > 0);56 int newQuantity = s->quantity + increase;57 int newBytes = newQuantity * s->size;58 int oldBytes = s->quantity*s->size;59 unsigned char* b = new unsigned char[newBytes];60 for(int i=0;i
storage[i];62 delete [](s->storage);63 s->storage = b;64 s->quantity = newQuantity;65 }66 67 void cleanup(CStash* s){68 if(s->storage != 0)69 {70 cout<<"freeing storage"<
storage;72 }73 }74 75 int main()76 {77 CStash intStash,stringStash;78 int i;79 char* cp;80 ifstream in;81 string line;82 const int bufsize = 80;83 initialize(&intStash,sizeof(int));84 for(i =0;i<100;i++)85 add(&intStash,&i);86 for(i=0;i

执行结果:

转载于:https://www.cnblogs.com/xing901022/archive/2012/10/10/2718791.html

你可能感兴趣的文章
Nginx配置文件nginx.conf中文详解(总结)
查看>>
Mysql出现Table 'performance_schema.session_status' doesn't exist
查看>>
MySQL innert join、left join、right join等理解
查看>>
sdc时序约束
查看>>
NoC片上网络
查看>>
开源SoC整理
查看>>
【2020-3-21】Mac安装Homebrew慢,解决办法
查看>>
influxdb 命令行输出时间为 yyyy-MM-dd HH:mm:ss(年月日时分秒)的方法
查看>>
已知子网掩码,确定ip地址范围
查看>>
判断时间或者数字是否连续
查看>>
docker-daemon.json各配置详解
查看>>
Docker(一)使用阿里云容器镜像服务
查看>>
Docker(三) 构建镜像
查看>>
FFmpeg 新旧版本编码 API 的区别
查看>>
RecyclerView 源码深入解析——绘制流程、缓存机制、动画等
查看>>
Android 面试题整理总结(一)Java 基础
查看>>
Android 面试题整理总结(二)Java 集合
查看>>
学习笔记_vnpy实战培训day02
查看>>
学习笔记_vnpy实战培训day03
查看>>
VNPY- VnTrader基本使用
查看>>