DoubleLi

qq: 517712484 wx: ldbgliet

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  4737 随笔 :: 2 文章 :: 542 评论 :: 1615万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

C++中map容器提供一个键值对容器,map与multimap差别仅仅在于multiple允许一个键对应多个值。本文主要总结一下map基本用法和嵌套用法示例。

一、map基本用法
  1   头文件 
  #include   <map> 

 

  2   定义 
  map<int,   int>   my_Map; //注意这里的int和int可以是其他类型
  或者是typedef     map<int,   int>   MY_MAP; 
  MY_MAP   my_Map; 

 

  3   插入数据 
  (1)   my_Map[1]   =   1; 
  (2)   my_Map.insert(map<int, int>::value_type(2,2)); 
  (3)   my_Map.insert(pair<int,int>(3,3)); 
  (4)   my_Map.insert(make_pair<string,int>(4,4)); 
  
  4   查找数据和修改数据 
  (1)   int   i   =   my_Map[1]; 
            my_Map[1]   =   i; 
  (2)   MY_MAP::iterator   my_Itr; 
            my_Itr.find(2); 
            int   j   =   my_Itr->second; 
            my_Itr->second   =   j; 
注意:

A.键本身是不能被修改的,除非删除。 

B.不管键存不存在,比如my_Map[1]   =   i;,都会执行赋值操作。
  
  5   删除数据 
  (1)   my_Map.erase(my_Itr); 
  (2)   my_Map.erase(3); 

  
  6   遍历数据 
  for   (my_Itr=my_Map.begin();   my_Itr!=my_Map.end();   ++my_Itr)   {} 
  
  7   其它方法 
  my_Map.size() :返回元素数目 
  my_Map.empty():判断是否为空 
  my_Map.clear() :清空所有元素 


二、嵌套用法

1.示例如下:


[cpp]  view plain copy

map<int,map<int,int> >multiMap; //对于这样的map嵌套定义,    
map<int, int> temp;    //定义一个map<int, string>变量,对其定义后在插入multiMap    
temp[9] = 9;    
temp[10] = 10;    
multiMap[10] = temp;    
multiMap[10][11]=11;     
multiMap[5][30]=30;    
map<int,map<int,int> >::iterator multitr;  // 以下是如何遍历本multiMap    
map<int,int>::iterator intertr;    
for(multitr=multiMap.begin();multitr!=multiMap.end();multitr++)    
{   
    for(intertr= multitr ->second.begin(); intertr != multitr ->second.end(); intertr ++)    
        cout<< multitr ->first<<" "<<intertr->first<<" ("<<intertr -> second <<")"<<endl;    
}   

2.

也可以这样:

[cpp]  view plain copy

map<int,map<int,int>* >multiMap;  
map<int, int>* temp = new map<int, int>;  
multiMap[10]=temp;  

这样动态new内存,就要记得delete,否则会有内存泄露,delete如下:

[cpp]  view plain copy

map<int, int>* temp1;  
for(multitr=multiMap.begin();multitr!=multiMap.end();multitr++)    
{   
    temp1 = multitr ->second;  
        delete  temp1;  
        temp1 = NULL;  
}   


转载请注明原创链接:http://blog.csdn.net/wujunokay/article/details/12163549

map的嵌套特别注意:
1,对于 std::map(std::string, std::map<std::string, string>) 这个类型的赋值和插入方法:
std::map<std::string, std::string> neimap;
std::map<std::string, std::map<std::string, std::string> > waimap;
std::string group = "groupp";
waimap[group] = neimap;
 
std::string key = "keyy";
std::string value = "valuee";
waimap[group].insert(std::make_pair(key.c_str(), value.c_str()));
注意必须用:   waimap[group].insert(std::make_pair(key.c_str(), value.c_str())); 给内层的map赋值(make_pair中的std::string要写作char类型),否则,在有些编译器下是编译不过的。

posted on   DoubleLi  阅读(3079)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2021-12-06 linux里grep和egrep,fgrep的区别
2012-12-06 WM_NOTIFY消息流程实例分析 .
2012-12-06 c++数据类型万能转换器boost::lexical_cast .
2012-12-06 WM_CTLCOLOR消息 .
2012-12-06 MFC经典好博文 .
2012-12-06 解说Win32的窗口子类化
2012-12-06 MFC消息处理流程概述 .
点击右上角即可分享
微信分享提示