鱼C论坛

 找回密码
 立即注册
查看: 2507|回复: 3

[技术交流] 分享一个c++缓存模板类,可以打败一大批作业的类

[复制链接]
发表于 2014-7-9 21:36:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
老师老师让同学写个学生成绩管理系统,可以保存到文件
今天写了个类,通用性高
分享


  1. #include<stdio.h>
  2. #include "cache.h"


  3. struct stkey
  4. {
  5. char szKey[20];
  6. bool operator < (const stkey &c) const
  7. {
  8. int iRev = strcmp(szKey,c.szKey);
  9. if(iRev<0)
  10. return true;

  11. return false;
  12. }

  13. void print()
  14. {
  15. printf("key:%s\t",szKey);
  16. }
  17. };

  18. template <class K>
  19. struct student
  20. {
  21. K key;
  22. int nNum;
  23. int nSocre;
  24. char szAsin[50];

  25. void print()
  26. {
  27. key.print();
  28. printf("学号:%d,分数:%d,个性签名:%s\n",nNum,nSocre,szAsin);
  29. }
  30. };

  31. int main(void)
  32. {
  33. //Cache<int,struct student<int> > cache_fist;

  34. Cache<struct stkey,struct student<struct stkey> > cache;

  35. #if 0
  36. for (int i=0;i<100;i++)
  37. {
  38. struct student<struct stkey> st = {0};
  39. sprintf(st.key.szKey,"学生%d",i+1);
  40. st.nNum = i+1;
  41. st.nSocre = i+100;
  42. sprintf(st.szAsin,"因为专注所以专业_%d",i+1);
  43. cache.add(st);
  44. }

  45. struct stkey st={"学生2"};
  46. if (cache.del(st))
  47. {
  48. printf("%s删除成功\n",st.szKey);
  49. }

  50. if(cache.save("1.dat"))
  51. {
  52. printf("保存成功\n");
  53. }
  54. #else
  55. cache.load("1.dat");
  56. cache.print();

  57. #endif

  58. system("pause");

  59. return 0;
  60. }
复制代码



QQ图片20140709213423.jpg
完整的下载地址,含视频解说
游客,如果您要查看本帖隐藏内容请回复




小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-7-16 10:25:58 | 显示全部楼层
必须得顶呀~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-7-16 10:58:30 | 显示全部楼层
赞一个,学习了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-7-16 11:08:09 | 显示全部楼层
参与/回复主题关闭
RE: 分享一个c++缓存模板类,可以打败一大批作业的类 [修改]
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-10 14:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表