C++中的map嵌套应用

本文介绍C++ STL中map的基本用法及嵌套map的定义与遍历方法,对比insert与[]操作符的不同效果。

       最近开发中要用到STL,然后自己查看了一些资料,并写了一些代码。在使用<map>中,想起了如果是map嵌套,该如何应用呢?下面是我的coding内容:

        对于传统的map,我们只需要:

        #include<map>

        #include<iostream>

        int main()

        {

        map<int, string> scores;

        scores.insert(make_pair(100,"maxi"));

        scores[100]="MAXI";

        scores.insert(make_pair(300,"xiaoyu"));

        scores.insert(make_pair(300,"xiao"));

        scores[200]="xiaoma";

        map<int,string>::iterator pScores;

        for(pScores=scores.begin();pScores!=scores.end();pScores++)

        {

                std::cout<<pScores->first<<"  "<<pScores->second<<endl;

        }

       

         return 0;}

         结果输出:

         100  MAXI

         200 xiaoma

         300 xiaoyu

         由此可以看出,scores[100]="MAXI"会直接替换掉原来100map对应的value,而如果调用scores.insert()函数,则由于本map是单映射的,

        但如果我想定义嵌套的map并对它进行遍历,该如何进行呢:

        #include<map>

        #include<iostream>

        int main()

        {

              map<int,map<int,string> >multiMap; //对于这样的map嵌套定义,有两种插入方法:

              map<int, string> temp;  //定义一个map<int, string>变量,对其定义后在插入multiMap

              temp.insert(make_pair(90,"hi"));

              temp.insert(pair<int,string>(100,"maxi)); //pair<int,string>()和make_pair()有相同作用

              multiMap.insert(make_pair(10, temp));   //将临时变量插入到multiMap中

              multiMap[10][80]="xiaoyu"; //可以直接赋值

              mulitMap[5][30]="xiaoma";

             

              map<int,map<int,string> >::iterator multitr;  // 以下是如何遍历本multiMap
              map<int,string>::iterator intertr;
              for(multitr=multiMap.begin();multitr!=multiMap.end();multitr++)
              {
                   for(intertr=
multitr ->second.begin(); intertr != multitr ->second.end(); intertr ++)
                    std::cout<<
multitr ->first<<" "<<intertr->first<<" ("<< intertr -> second <<")"<<endl;
              }

              return 0;

          } 

    

          运行结果如下:

          

              5 30 (xiaoma)
             10 80 (xiaoyu)
             10 90 (hi)
             10 100 (maxi)

          总结,map的成员加入有两种赋值方法,一种是调用map.insert()函数,这样,由于是单映射,后面加入的新的pair对如果有key值和前面一样,那么后面的pair对元素将不会被加入到map中;但如果是直接[ ]=赋值操作的话,相当于数组赋值,会直接替换掉原来具有相同key域的pair对。本发现会对如何增加pair对数据的调用方法有些指导意义。

             

        

        

### C++ 中 `map` 容器嵌套 `array` 在 C++ 中,可以将标准库中的 `std::array` 作为 `std::map` 的值类型来创建键值对结构。下面展示了一个具体的例子,其中 `std::map<int, std::array<int, 3>>` 表示整数到三个整数组成的数组之间的映射。 #### 创建和初始化 ```cpp #include <iostream> #include <map> #include <array> void printMap(const std::map<int, std::array<int, 3>>& m) { for (const auto& pair : m) { std::cout << "Key: " << pair.first << ", Array Values: "; for (auto value : pair.second) { std::cout << value << ' '; } std::cout << '\n'; } } int main() { // 初始化 map 并插入一些元素 std::map<int, std::array<int, 3>> myMap; // 插入新条目 myMap.insert({1, {10, 20, 30}}); myMap.emplace(2, std::array<int, 3>({40, 50, 60})); // 修改现有条目的值 myMap[3] = {70, 80, 90}; // 打印所有项 printMap(myMap); return 0; } ``` 这段代码展示了如何定义一个从整型到固定大小为三的整形数组的地图,并向其添加几个元素[^1]。 #### 访问和修改 访问存储于 `map` 内部的 `array` 成员可以通过下标运算符完成: ```cpp // 获取特定 key 对应的 array 并更新其中一个元素 if (myMap.count(2)) { (*myMap.find(2)).second.at(1) = 55; // 更新第二个位置上的数值 } printMap(myMap); ``` 此片段说明了怎样安全地检查某个键是否存在以及如何更改关联数组的内容[^5]。 #### 删除操作 对于移除指定键所对应的记录,可以直接调用 erase 方法: ```cpp // 移除具有给定 key 的 entry size_t removedCount = myMap.erase(1); // 尝试删除 key=1 的 entry std::cout << "Removed entries count: " << removedCount << "\n"; printMap(myMap); ``` 上述代码段解释了如何利用 erase 函数依据键名去除相应的键值对并获取实际被清除的数量.
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值