(转)std::map的用法总结

本文介绍了C++标准库中map容器的基本用法,包括插入、查找、删除和遍历等操作,并详细说明了如何使用自定义结构作为键以及如何实现双键或多键的map。

给出了map的基本用法如插入、查找、删除、遍历等等,同时告诉你如何实现双键map,包括 
(1) 只有两个键都匹配才命中目标
(2) 两个键中任意一个匹配就命中目标

可以扩展到多键

(一) 介绍
特点:
1.map将Key的object和T的Object绑定到一起,因此是一种Pair Associative Container, 表示其value type为 pair。
2.它同时也是Unique Associative Container,表示没有两个元素具有相同的Key。
3.它还是一种Sorted Associative Container,因此第三个参数只能是less,greater之类的functor, 相比较而言,
  hash table是 equal_to, not_equal_to之类的functor。
(二) 基本用法
通过以下范例,可以看出map的一些基本用法: 插入、查找、删除、遍历等等。

复制代码
 1  #if  defined (_MSC_VER)
 2  #pragma  warning(disable: 4786)
 3  #endif
 4  #include  < iostream >
 5  #include  < map >
 6  #include  < algorithm >
 7  int  main( int  argc,  char   * argv[])
 8  {
 9     /*  define a map  */
10    std::map _map;
11     
12     /*  insert  */
13    _map.insert( std::map::value_type( 0 32.8 ) );
14    _map.insert( std::map::value_type( 1 33.2 ) );
15    _map.insert( std::map::value_type( 2 35.8 ) );
16    _map.insert( std::map::value_type( 3 36.4 ) );
17    _map.insert( std::map::value_type( 4 37.8 ) );
18    _map.insert( std::map::value_type( 5 35.8 ) );
19     
20     /*  这个是常用的一种map赋值方法  */
21    _map[ 7 =   245.3 ;
22     
23     /*  find by key  */
24    std::map::iterator itr;
25    itr  =  _map.find( 4 );
26     
27     if ( itr  !=  _map.end() )
28    {
29    std::cout  <<   " Item: "   <<  itr -> first  <<   "  found, content:  "   <<  itr -> second  <<  std::endl;
30    }
31     
32    std::cout  <<  std::endl;
33     
34     /*  delete item from map  */
35     if ( itr  !=  _map.end() )
36    {
37    _map.erase(itr);
38    }
39     
40     /*  travel through a map  */
41    std::map::iterator itr1  =  _map.begin();
42     for ( ; itr1  !=  _map.end();  ++ itr1 )
43    {
44    std::cout  <<   " Item: "   <<  itr1 -> first  <<   " , content:  "   <<  itr1 -> second  <<  std::endl;
45    }
46     
47    std::cout  <<  std::endl;
48     
49     /*  empty a map  */
50    _map.clear();
51     
52     return   0 ;
53  }
复制代码


(三) 当Key是结构时该如何定义结构
比如 Key是结构MyStruct类型, 此时map可以定义如下:
std::map > _map;
其中Compare 缺省是std::less,这里可以不写,自定义的结构必须实现Compare指定的比较操作,因此自定义结构
MyStruct必须按照如下写法:

复制代码
1  struct  MyStruct
2  {
3     int  key;
4     
5     bool   operator   <  (  const  MyStruct rhs)  const
6    {
7     return  key  <  rhs.key;
8    }
9  };
复制代码

当然也可以实现全局operator <

1  bool   operator   <  (  const  MyStruct lhs,  const  MyStruct rhs) 
2  {
3     return  lhs.key  <  rhs.key;
4  }

另外,当Compare 是std::greater时,需要实现 operator >
(四) 如何实现两个Key的map, 只有两个Key都匹配才命中目标
可以定义结构MyStruct如下:
复制代码
 1  struct  MyStruct
 2  {
 3     int  key1;
 4     double  key2
 5     
 6     bool   operator   <  (  const  MyStruct rhs)  const
 7    {
 8     /*  两个key必须都匹配才命中  */
 9     return  ( key1  <  rhs.key1  ||  key2  <  rhs.key2 );
10    }
11  };
复制代码

(五) 如何实现两个Key的map, 两个Key中任意一个匹配就命中目标
可以定义结构MyStruct如下:

复制代码
要将 `std::map<std::string, std::map<std::string, std::map<std::string, std::map<std::string, std::map<std::string, std::vector<CXHFCODE>>>>>>` 替换为使用 `CString` 作为键的结构,同时保持嵌套层次不变,需要对每一层的 `std::map<std::string, ...>` 进行替换。 具体做法是将所有以 `std::string` 为键的 `std::map` 替换为以 `CString` 为键的 `std::map`。由于 `CString` 是 MFC 中的字符串类,其重载了比较运算符(如 `<`),因此可以直接用于 `std::map` 的键类型而无需额外提供比较函数。 以下是替换前后的结构对比: 替换前: ```cpp std::map<std::string, std::map<std::string, std::map<std::string, std::map<std::string, std::map<std::string, std::vector<CXHFCODE>>>>>> ``` 替换后: ```cpp std::map<CString, std::map<CString, std::map<CString, std::map<CString, std::map<CString, std::vector<CXHFCODE>>>>>> ``` 遍历该结构时,需显式声明迭代器类型并使用 `CString` 类型的键进行访问: ```cpp typedef std::map<CString, std::map<CString, std::map<CString, std::map<CString, std::map<CString, std::vector<CXHFCODE>>>>>> NestedCStringMap; NestedCStringMap regions_; for (NestedCStringMap::iterator itProvince = regions_.begin(); itProvince != regions_.end(); ++itProvince) { const CString& provinceKey = itProvince->first; const std::map<CString, std::map<CString, std::map<CString, std::map<CString, std::vector<CXHFCODE>>>>>& cityMap = itProvince->second; for (std::map<CString, std::map<CString, std::map<CString, std::map<CString, std::vector<CXHFCODE>>>>>::iterator itCity = cityMap.begin(); itCity != cityMap.end(); ++itCity) { const CString& cityKey = itCity->first; const std::map<CString, std::map<CString, std::map<CString, std::vector<CXHFCODE>>>>& districtMap = itCity->second; for (std::map<CString, std::map<CString, std::map<CString, std::vector<CXHFCODE>>>>::iterator itDistrict = districtMap.begin(); itDistrict != districtMap.end(); ++itDistrict) { const CString& districtKey = itDistrict->first; const std::map<CString, std::map<CString, std::vector<CXHFCODE>>>& townMap = itDistrict->second; for (std::map<CString, std::map<CString, std::vector<CXHFCODE>>>::iterator itTown = townMap.begin(); itTown != townMap.end(); ++itTown) { const CString& townKey = itTown->first; const std::map<CString, std::vector<CXHFCODE>>& villageMap = itTown->second; for (std::map<CString, std::vector<CXHFCODE>>::iterator itVillage = villageMap.begin(); itVillage != villageMap.end(); ++itVillage) { const CString& villageKey = itVillage->first; const std::vector<CXHFCODE>& codes = itVillage->second; for (const CXHFCODE& code : codes) { // 执行 SQL 插入操作 } } } } } } ``` 上述代码通过逐层定义迭代器和引用类型,确保在不使用 `auto` 的情况下正确访问每层数据[^3]。此方法保留了原始结构的嵌套逻辑,并利用 `CString` 提供的比较语义来保证 `std::map` 正常运行[^4]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值