【问题标题】:C++Map :Accessing map inside a mapC++Map:访问地图内的地图
【发布时间】:2023-04-10 13:42:01
【问题描述】:

如何在 C++ 中通过 std::map 循环、访问和分配?我的地图定义为:

std::map<std::string, std::map<std::string, int>>

例如,上面的容器保存这样的数据:

m["country"]["capital"] = value;
m["country1"]["capital1"] = value;
m["country2"]["capital2"] = value;

根据国家和首都,天气值得到更新

目前是否使用and map

map<string,int>mp;                                                                                              
if(Albania)
map[Tirana]= weathervalue;
if("Algeria")
map["Algiers"] = weathervalue;

欢迎任何优化提示、指示和想法

【问题讨论】:

  • 欢迎来到stackoverflow!您能否详细说明第一种和第二种地图类型是如何一起使用的?第二个示例具有 int 值,但在第二个示例中,它是 std::string?
  • 抱歉错字,它实际上是 std::map >
  • 输入数据从何而来?字符串和整数?
  • weather(int)和capital(string)是从底层获取的。例如,如果从下层收到的国家是阿尔及利亚,则为该国家的首都(本地)分配天气值(从下层)
  • 好的,但是作为什么? std::vector&lt;std::string&gt;std::vector&lt;int&gt;?您要求在地图上进行循环,但我认为我们还需要知道要放入地图的数据所在的位置,以便回答您的​​问题。

标签: c++ dictionary mapping nested-map


【解决方案1】:

你可以这样遍历

   std::map< std::string, std::map<std::string, int> > myMap;
    
    for(auto& iter : myMap)
    {
        for(auto& childIter : iter.second)
        {
            // do here what you want i.e
            if(childIter.first == "xyz")
            childIter.second = 20;
        }
    }
// OR
    for(auto& [country, map] : myMap)
    {
      for(auto& [capital , weather] : map)
      {
        if(capital == "xyz")
          weather = 20;
      }
    }

另一种方式

 typedef map<string,int> innermap;
 std::map< std::string, innermap > myMap;

 for(auto iterOuter = myMap.begin(); iterOuter != myMap.end(); iterOuter++)
    {
        for(map<string, int>::iterator iterInner = iterOuter->second.begin(); iterInner  != iterOuter->second.end(); iterInner++)
        {
            // do here what you want
        }
    }

【讨论】:

  • 如果地图定义如下 typedef map innermap; map outermap .. 如何分配、访问和循环。
  • 你的意思是map&lt;string, innermap&gt; outermap。您可以以相同的方式访问和分配。
  • 谢谢,但是如何访问以及如何循环通过相同的typedef map mp map outermap
  • map ::iterator it map ::iterator inner_it for(it =mp.begin();it!=mp.end();it++)- -- 抛出错误
  • 你可以像这样使用 iter for(auto iterOuter = myMap.begin(); iterOuter != myMap.end(); iterOuter++) { for(map::iterator iter = iterOuter->second.begin(); iter != iterOuter->second.end(); iter++) { // 在这里做你想做的 } }
【解决方案2】:
for(auto& it:mp){
   for(auto& it1: it.second){
        it1.second=weathervalue;
}

您可以向此循环添加条件,但这是在地图内遍历地图的基本方式。

基本上,map 是键值对的结构,因此在遍历它时,您可以这样对待它。嵌套映射是原始映射的键值对的值部分,因此它被访问为 (name_of_iterator)。 第二个。

正如 m88 建议的那样: 增加代码的清晰度

  for(auto& [country,submap]:mp){
     for(auto& [capital,wetherValue]: submap){
          wetherValue=New_Weather_Value;
  }

这是 C++17 标准中添加的结构化绑定的特性。

回答附加问题。

  typedef map<string,int> mp; 
//note that you gave the alias mp to the map so you can not use it as as variable name
    void function(){
          int new_value=50; // some value
          mp::iterator inner; // iterator of inner map
          map<int,mp> mymap; // main map
          map<int,mp>::iterator outer; // iterator of the main map
          for (outer=mymap.begin();outer!=mymap.end();outer++){
            for(inner=outer->second.begin();inner!=outer->second.end();inner++)
               inner->second= new_value;
              //do some other stuff
           }
    
    }
//Additionally note that you must use the smart pointer in the case of nested maps

这是一种方法,但您也可以使用前两个代码 sn-ps(因为关键字 auto 检测正在使用的变量类型)。

【讨论】:

  • 如果 OP 使用 C++17,甚至可能是结构化绑定,因此更易于阅读。类似于for (auto&amp; [country, submap]: mp)for (auto&amp; [capital, weatherVal]: submap)
  • 如果地图定义如下 typedef map innermap; map outermap .. 如何分配、访问和循环。
  • 你的意思是第二部分的innermap吗? (mapoutermap)
  • @annmathew map["foo"] 如果条目不存在,则创建该条目并返回对其的引用。 map.at("foo") 如果不存在则抛出异常,或者返回对其的引用。 map.find("foo") 在条目上返回一个迭代器,或者 map.end() 如果它不存在。查看文档:en.cppreference.com/w/cpp/container/map
  • 谢谢,但是如何访问以及如何循环同一个typedef map mp map outermap
猜你喜欢
  • 2018-08-31
  • 1970-01-01
  • 1970-01-01
  • 2020-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多