免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1384 | 回复: 5
打印 上一主题 下一主题

写了一个变态模板,对模板有兴趣的可以看看. [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-09-30 13:05 |只看该作者 |倒序浏览

  1. #pragma warning(disable:4786)
  2. #include <conio.h>;
  3. #include <iostream>;
  4. #include <string>;
  5. #include <list>;
  6. #include <map>;
  7. #include <vector>;
  8. #include <algorithm>;
  9. #include <functional>;
  10. using namespace std;

  11. template <class _T>;

  12. struct empty_op
  13. {
  14.         void operator()(_T val){}
  15. };

  16. template<class _T1,class _T2=empty_op<_T1>; >;
  17. struct clear_obj_1d
  18. {
  19.         _T2 _op;
  20.         void operator()(_T1 p)
  21.         {
  22.                 _op(p);
  23.                 delete p;
  24.         }
  25. };

  26. template<class _T1,class _T2=empty_op<_T1>; >;
  27. struct clear_obj_2d{
  28.         _T2 _op;
  29.         void operator()(_T1 val)
  30.         {
  31.                 _op(val);
  32.                 delete val.second;
  33.         }
  34. };

  35. template<class _T1,class _T2>;
  36. struct clear_1d{
  37.         void operator()(_T1 p)
  38.         {
  39.                 for_each(p->;begin(),p->;end(),_T2());
  40.         }
  41. };


  42. template<class _T1,class _T2>;
  43. struct clear_2d{
  44.         void operator()(_T1 p)
  45.         {
  46.                 for_each(p.second->;begin(),p.second->;end(),_T2());
  47.         }
  48. };

  49. typedef list<int *>;                                int_p_l;
  50. typedef list<int_p_l *>;                        int_p_l_p_l;
  51. typedef list<int_p_l_p_l *>;                btl;

  52. int main()
  53. {
  54.         btl l;
  55.         for_each(l.begin(),l.end(),\
  56.                 clear_obj_1d<btl::value_type,\
  57.                         clear_1d<btl::value_type,\
  58.                                 clear_obj_1d<int_p_l_p_l::value_type,
  59.                                         clear_1d<int_p_l_p_l::value_type,
  60.                                                 clear_obj_1d<int_p_l::value_type
  61.                                                 >;
  62.                                         >;
  63.                                 >;
  64.                         >;
  65.                 >;());
  66.         getch();
  67.         return 0;
  68. }

复制代码

论坛徽章:
0
2 [报告]
发表于 2005-09-30 13:08 |只看该作者

写了一个变态模板,对模板有兴趣的可以看看.


  1. int_p_l pl;
  2.         pl.push_back(new int(2));
  3.         pl.push_back(new int(24));
  4.         pl.push_back(new int(3));
  5.         for_each(pl.begin(),pl.end(),clear_obj_1d<int*>;());

复制代码

论坛徽章:
0
3 [报告]
发表于 2005-09-30 13:09 |只看该作者

写了一个变态模板,对模板有兴趣的可以看看.

头一次见。
那里用这个样子写,把一个循环写的这样复杂

论坛徽章:
0
4 [报告]
发表于 2005-09-30 13:15 |只看该作者

写了一个变态模板,对模板有兴趣的可以看看.

为了BT嘛,呵呵,刚开始只是想写一个
  for_each(pl.begin(),pl.end(),clear_obj_1d<int*>;());  这样的东西.

但是后来想到, 如果容器内部是指向另一个容器的指针,想看看有没有办法,用模板逐级释放.模板到是写了,就是调用比较费脑子.哈哈

论坛徽章:
0
5 [报告]
发表于 2005-09-30 13:43 |只看该作者

写了一个变态模板,对模板有兴趣的可以看看.

哈哈,那么多()虫灾
佩服

论坛徽章:
0
6 [报告]
发表于 2005-09-30 13:51 |只看该作者

写了一个变态模板,对模板有兴趣的可以看看.

虽然看不懂,还是表示一下钦佩
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP