Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1844260
  • 博文数量: 274
  • 博客积分: 2366
  • 博客等级: 大尉
  • 技术积分: 1880
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-22 09:37
文章分类

全部博文(274)

文章存档

2022年(1)

2020年(10)

2019年(7)

2018年(18)

2017年(26)

2016年(32)

2015年(43)

2014年(30)

2013年(44)

2012年(36)

2011年(17)

2010年(10)

分类: C/C++

2013-12-31 15:18:03


点击(此处)折叠或打开

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <time.h>
  4. #include <iostream>
  5. #include <string>
  6. #include <list>

  7. using namespace std;

  8. class simplea
  9. {
  10. public:
  11.     simplea(int a);
  12.     virtual ~simplea();
  13.     int yes();
  14.     void dump1();

  15. private:
  16.     int value;
  17. };

  18. simplea::simplea(int a):value(a)
  19. {
  20. }

  21. simplea::~simplea()
  22. {
  23.     printf("%u \n", value);
  24. }

  25. int simplea::yes()
  26. {
  27.     if(value > 10)
  28.     {
  29.         return 1;
  30.     }
  31.     return 0;
  32. }

  33. void simplea::dump1()
  34. {
  35.     printf("%u ", value);
  36. }

  37. unsigned int simplerand(unsigned int max)
  38. {

  39.     return (unsigned int)rand() % max;
  40. }

  41. std::list<simplea*> slist;


  42. int _tmain(int argc, _TCHAR* argv[])
  43. {
  44.     int i = 0;
  45.     int v = 0;

  46.     printf("create value:\n");

  47.     srand((unsigned)time(NULL));

  48.     for(;i < 10;i++)
  49.     {
  50.         v = simplerand(15);
  51.         printf("%u ", v);
  52.         simplea* ptmp = new simplea(v);
  53.         slist.push_back(ptmp);
  54.     }
  55.     printf("\n");
  56.   
  57.     std::list<simplea*>::iterator itList;
  58.     for( itList = slist.begin(); itList != slist.end(); )
  59.     {
  60.         simplea* tmp = *itList;

  61.         if(tmp->yes())
  62.         {
  63.             itList = slist.erase(itList);
  64.             delete tmp;
  65.         }
  66.         else
  67.             itList++;
  68.     }
  69.     
  70.     printf("after delete some\n");
  71.     for( itList = slist.begin(); itList != slist.end(); itList++)
  72.     {
  73.         simplea* tmp = *itList;
  74.         tmp->dump1();
  75.         
  76.     }
  77.     printf("\n");

  78.     printf("after delete all\n");
  79.     
  80.     for( itList = slist.begin(); itList != slist.end();)
  81.     {
  82.         simplea* tmp = *itList;
  83.         itList = slist.erase(itList);
  84.         delete tmp;
  85.     }
  86.     
  87.     for( itList = slist.begin(); itList != slist.end(); itList++)
  88.     {
  89.         simplea* tmp = *itList;
  90.         tmp->dump1();
  91.     }

  92.     printf("\n");
  93.     printf("end\n");
  94.     printf("\n");

  95.     return 0;
  96. }

阅读(1205) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~