访问嵌套地图

时间:2018-09-07 09:37:49

标签: c++ data-structures stdmap

我尝试使用包含4个嵌套地图的地图。问题是我需要在每个地图中找到一个键并获取上一个嵌套地图的值。

地图:

typedef map<int , map<int , map<int , map<int , map <int, string> >  > > > g_tAllGroupMsgTextMap ;

上方是地图。如何访问最后一个嵌套地图(map<int, string>

string getText(int GroupId,int NetworkIdIndex,int LangCodeIndex,int MapVersion,int TagCode)
{

   typedef map<int , map<int , map<int , map<int , map <int, string> >  > > > g_tAllGroupMsgTextMap ;
   g_tAllGroupMsgTextMap g_AllGroupMsgTextMap;
   string l_sMsgTextResponseString;
   typedef map<int , map<int , map<int , map <int, string> >  > > g_tFirstInnerMap;
   typedef  map<int , map<int , map <int, string> >  >  g_tSecondInnerMap;
   typedef  map<int , map <int, string> >    g_tThirdInnerMap;
   typedef  map <int, string>  g_tFourthInnerMap;

   g_tFirstInnerMap g_FirstMap;
   g_tSecondInnerMap g_SecondMap;
   g_tThirdInnerMap g_ThirdMap;
   g_tFourthInnerMap g_FourthMap;

   g_tAllGroupMsgTextMap :: iterator MsgTextMapItr;
   g_tFirstInnerMap :: iterator FirstInnerMapItr;
   g_tSecondInnerMap :: iterator SecondInnerMapItr;
   g_tThirdInnerMap :: iterator ThirdInnerMapItr;
   g_tFourthInnerMap :: iterator FourthInnerMapItr;

   MsgTextMapItr =  g_AllGroupMsgTextMap.find(GroupId);
   if(MsgTextMapItr != g_AllGroupMsgTextMap.end())
   {
      FirstInnerMapItr= MsgTextMapItr->second.find(NetworkIdIndex);
      if( FirstInnerMapItr != MsgTextMapItr->second.end())
      {
         SecondInnerMapItr = FirstInnerMapItr->second.find(LangCodeIndex); 
         if( SecondInnerMapItr != FirstInnerMapItr->second.end())
         {
            ThirdInnerMapItr=SecondInnerMapItr->second.find(MapVersion);
            if(ThirdInnerMapItr != SecondInnerMapItr->second.end())
            {
               FourthInnerMapItr = ThirdInnerMapItr->second.find(TagCode);
               if(FourthInnerMapItr != ThirdInnerMapItr->second.end())
               {
                  cout<<"MsgText ->"<<FourthInnerMapItr->second<<endl;
                  //return FourthInnerMapItr->second;
               }
               else
               {
                  cout<<"not found";

               }
            }
         }
      }
   }
}

我既没有得到FourthInnerMapItr->second也没有得到not found 有什么需要修改的吗?

1 个答案:

答案 0 :(得分:0)

g_tAllGroupMsgTextMap[key],您将获得第一张地图。然后再次执行相同操作,以获取第二张嵌套地图并继续。