1. // union_test.cpp : 定义控制台应用程序的入口点。
  2. //
  3. /*
  4. 结论:
  5. typedef std::map<int, desktop*> deskMap; 存放的是指针,也就是到时候指针指向的内存如果改变,则结果也改变
  6. typedef std::map<int, desktop> deskMap2; //存放的是数据,


  7. */

  8. #include "stdafx.h"
  9. #include <map>
  10. #include <iostream>


  11. typedef struct _desk{
  12.     int legs;
  13.     int id;
  14. }desktop;

  15. /* 注意值一个是存指针,一个是存数据 */
  16. typedef std::map<int, desktop*> deskMap;
  17. typedef std::map<int, desktop> deskMap2;

  18. using namespace std;

  19. /*结果
  20. 024
  21. 124
  22. 224
  23. */
  24. void no_malloc_test()
  25. {
  26.     deskMap map;
  27.     deskMap::iterator iter;
  28.     desktop desk, *p;
  29.     int i;


  30.     for (i = 0; i < 3; i++)
  31.     {
  32.         desk.id = i;
  33.         desk.legs = i * i;
  34.         map.insert(deskMap::value_type(i, &desk));
  35.     }

  36.     for (iter = map.begin(); iter != map.end(); iter++)
  37.     {
  38.         p = iter->second;
  39.         cout << iter->first << p->id << p->legs <<endl;
  40.     }
  41.     map.clear();
  42. }

  43. /*
  44. 结果:
  45. 000
  46. 111
  47. 224
  48. */
  49. void no_malloc_test2()
  50. {
  51.     deskMap2 map;
  52.     deskMap2::iterator iter;
  53.     desktop desk, *p;
  54.     int i;


  55.     for (i = 0; i < 3; i++)
  56.     {
  57.         desk.id = i;
  58.         desk.legs = i * i;
  59.         map.insert(deskMap2::value_type(i, desk));
  60.     }

  61.     for (iter = map.begin(); iter != map.end(); iter++)
  62.     {
  63.         p = (desktop*)&iter->second;
  64.         cout << iter->first << p->id << p->legs <<endl;
  65.     }
  66.     map.clear();
  67. }


  68. /*
  69. 结果:改变的值会保存
  70. 000
  71. 111
  72. 224
  73. 010
  74. 121
  75. 234
  76. */
  77. void no_malloc_modify_test2()
  78. {
  79.     deskMap2 map;
  80.     deskMap2::iterator iter;
  81.     desktop desk, *p;
  82.     int i;


  83.     for (i = 0; i < 3; i++)
  84.     {
  85.         desk.id = i;
  86.         desk.legs = i * i;
  87.         map.insert(deskMap2::value_type(i, desk));
  88.     }

  89.     for (iter = map.begin(); iter != map.end(); iter++)
  90.     {
  91.         p = (desktop*)&iter->second;
  92.         cout << iter->first << p->id << p->legs <<endl;
  93.         p->id++;//modify
  94.     }

  95.         for (iter = map.begin(); iter != map.end(); iter++)
  96.     {
  97.         p = (desktop*)&iter->second;
  98.         cout << iter->first << p->id << p->legs <<endl;
  99.     }
  100.     map.clear();
  101. }

  102. void new_test()
  103. {
  104.     deskMap map;
  105.     deskMap::iterator iter;
  106.     desktop *p;
  107.     desktop *pdesk1 = new desktop;
  108.     desktop *pdesk2 = new desktop;
  109.     desktop *pdesk3 = new desktop;

  110.     pdesk1->id = 0;
  111.     pdesk1->legs = 0;

  112.     pdesk2->id = 1;
  113.     pdesk2->legs = 1;

  114.     pdesk3->id = 2;
  115.     pdesk3->legs = 4;


  116.     map.insert(deskMap::value_type(1, pdesk1));
  117.     map.insert(deskMap::value_type(2, pdesk2));
  118.     map.insert(deskMap::value_type(3, pdesk3));
  119.     
  120.     /* 删除pdesk1, 输出结果如下。也就是指向同一块内存*/
  121.     //delete pdesk1;
  122. #if 0
  123. 1-572662307-572662307
  124. 211
  125. 324
  126. #endif
  127.     

  128.     for (iter = map.begin(); iter != map.end(); iter++)
  129.     {
  130.         p = iter->second;
  131.         cout << iter->first << p->id << p->legs <<endl;
  132.     }
  133.     map.clear();
  134. }

  135. int _tmain(int argc, _TCHAR* argv[])
  136. {
  137.     no_malloc_modify_test2();

  138.     return 0;
  139. }

09-05 12:39