分享

c++ map使用(转)

 汇英四方 2014-01-27

最近学习中需要用到stl map和vector,所以此处就整理了下,把它封装成一个类,方便自己以后使用,后面会加上vector的使用


map_class.h代码:

  1. #ifndef MAP_CLASS_H  
  2. #define MAP_CLASS_H  
  3.   
  4. /* 
  5. C++ Maps是一种关联式容器,包含"关键字/值"对 
  6. Maps是没有扩充容量的,根据你的运行环境不同而不同,max_size()函数会告诉你在当前的机器上使用map的最大容量 
  7. map的基本操作函数: 
  8. begin()          返回指向map头部的迭代器 
  9. clear()         删除所有元素 
  10. count()          返回指定元素出现的次数 
  11. empty()          如果map为空则返回true 
  12. end()            返回指向map末尾的迭代器 
  13. equal_range()    返回特殊条目的迭代器对 
  14. erase()          删除一个元素 
  15. find()           查找一个元素 
  16. get_allocator()  返回map的配置器 
  17. insert()         插入元素 
  18. key_comp()       返回比较元素key的函数 
  19. lower_bound()    返回键值>=给定元素的第一个位置 
  20. max_size()       返回可以容纳的最大元素个数 
  21. rbegin()         返回一个指向map尾部的逆向迭代器 
  22. rend()           返回一个指向map头部的逆向迭代器 
  23. size()           返回map中元素的个数 
  24. swap()           交换两个map 
  25. upper_bound()    返回键值>给定元素的第一个位置 
  26. value_comp()     返回比较元素value的函数 
  27. */  
  28.   
  29. #include<map>  
  30. #include<string>  
  31. #include <stdio.h>  
  32.   
  33. using namespace std;  
  34.   
  35. class MapClass  
  36. {  
  37. public:  
  38.     //构造函数  
  39.     MapClass();  
  40.     //析构函数  
  41.     ~MapClass();  
  42.     //设置数据  
  43.     bool SetMapData(string key, int value);  
  44.     bool SetMapData(int key, string value);  
  45.     //获取数据  
  46.     int GetMapData(string key);  
  47.     string GetMapData(int key);  
  48.     //删除数据  
  49.     bool RemoveMapData(string key);  
  50.     bool RemoveMapData(int key);  
  51.     //打印Map信息  
  52.     void PrintMap();  
  53. private:  
  54.     //[string]=int  
  55.     map<string, int>MapString;  
  56.     //[int]=string  
  57.     map<int, string>MapInt;  
  58.     //限制MapString容量大小  
  59.     int MapStringMaxSize;  
  60.     //限制MapInt容量大小  
  61.     int MapIntMaxSize;  
  62. };  
  63.   
  64. #endif  


map_class.cpp代码:

  1. #include "map_calss.h"  
  2.   
  3. MapClass::MapClass()  
  4. {  
  5.     MapStringMaxSize = 500;  
  6.     MapIntMaxSize = 500;  
  7. }  
  8.   
  9. MapClass::~MapClass()  
  10. {  
  11.     if(!MapString.empty()){  
  12.         for(map<string, int>::iterator iter = MapString.begin(); iter != MapString.end(); iter++){  
  13.             MapString.erase(iter);  
  14.         }  
  15.         MapString.clear();  
  16.     }  
  17.       
  18.     if(!MapInt.empty()){  
  19.         for(map<int, string>::iterator iter = MapInt.begin(); iter != MapInt.end(); iter++){  
  20.             MapInt.erase(iter);  
  21.         }  
  22.         MapInt.clear();  
  23.     }  
  24. }  
  25.   
  26. bool MapClass::SetMapData(string key, int value)  
  27. {  
  28.     map<string ,int>::iterator iter;  
  29.     iter=MapString.find(key);  
  30.     if(iter != MapString.end()){  
  31.         iter->second = value;  
  32.         return true;  
  33.     }  
  34.     int size = MapString.size();  
  35.     if(size >MapStringMaxSize){  
  36.         return false;  
  37.     }  
  38.     MapString.insert(pair<string, int>(key, value));  
  39.     return true;  
  40. }  
  41.   
  42. bool MapClass::SetMapData(int key, string value)  
  43. {  
  44.     map<int ,string>::iterator iter;  
  45.     iter=MapInt.find(key);  
  46.     if(iter != MapInt.end()){  
  47.         MapInt.erase(iter);  
  48.     }  
  49.     else{  
  50.         int size = MapInt.size();  
  51.         if(size >MapIntMaxSize){  
  52.             return false;  
  53.         }  
  54.     }  
  55.     MapInt.insert(pair<int, string>(key, value));  
  56.     return true;  
  57. }  
  58.   
  59. int MapClass::GetMapData(string key)  
  60. {  
  61.     map<string ,int>::iterator iter;  
  62.     iter=MapString.find(key);  
  63.     if(iter == MapString.end()){  
  64.         return 0;  
  65.     }  
  66.     return iter->second;  
  67. }  
  68.   
  69. string MapClass::GetMapData(int key)  
  70. {  
  71.     map<int ,string>::iterator iter;  
  72.     iter=MapInt.find(key);  
  73.     if(iter == MapInt.end()){  
  74.         return NULL;  
  75.     }  
  76.     return iter->second;  
  77. }  
  78.   
  79. bool MapClass::RemoveMapData(string key)  
  80. {  
  81.     map<string ,int>::iterator iter;  
  82.     iter=MapString.find(key);  
  83.     if(iter == MapString.end()){  
  84.         return false;  
  85.     }  
  86.     MapString.erase(iter);  
  87.     return true;  
  88. }  
  89.   
  90. bool MapClass::RemoveMapData(int key)  
  91. {  
  92.     map<int ,string>::iterator iter;  
  93.     iter=MapInt.find(key);  
  94.     if(iter == MapInt.end()){  
  95.         return false;  
  96.     }  
  97.     MapInt.erase(iter);  
  98.     return true;  
  99. }  
  100.   
  101. void MapClass::PrintMap()  
  102. {  
  103.     if(!MapString.empty()){  
  104.         for(map<string, int>::iterator iter = MapString.begin(); iter != MapString.end(); iter++){  
  105.             printf("MapString  first:%s, second:%d \n", iter->first.c_str(), iter->second);  
  106.         }  
  107.     }  
  108.       
  109.     if(!MapInt.empty()){  
  110.         for(map<int, string>::iterator iter = MapInt.begin(); iter != MapInt.end(); iter++){  
  111.             printf("MapInt  first:%d, second:%s \n", iter->first, iter->second.c_str());  
  112.         }  
  113.     }  
  114. }  



例子:

  1. // maptest.cpp : 定义控制台应用程序的入口点。  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include "map_calss.h"  
  6.   
  7.   
  8. int _tmain(int argc, _TCHAR* argv[])  
  9. {  
  10.     MapClass* _mapObj = new MapClass();  
  11.     _mapObj->SetMapData("aaa", 1);  
  12.     _mapObj->SetMapData("bbb", 2);  
  13.     _mapObj->SetMapData(3, "ccc");  
  14.     _mapObj->SetMapData(4, "ddd");  
  15.     _mapObj->PrintMap();  
  16.   
  17.     string ccc = _mapObj->GetMapData(3);  
  18.     int aaa = _mapObj->GetMapData("aaa");  
  19.     printf("\nccc:%s aaa:%d\n\n", ccc.c_str(), aaa);  
  20.   
  21.     _mapObj->RemoveMapData("aaa");  
  22.     _mapObj->RemoveMapData(4);  
  23.     _mapObj->PrintMap();  
  24.   
  25.     return 0;  
  26. }  




    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多