是不正确插入嵌套地图会使地图下的新二叉搜索树?

时间:2014-05-14 14:29:35

标签: c++ map stl

我看到了一个人的示例代码,他使用嵌套地图存储到容器中。

 map< int,map<int,int> > outer_map;

ins_test[5].insert(make_pair<int,int>(5,6));

在上面插入地图中,

此部分map<int,int>ins_test[5]将返回对(reference).insert();的引用 将插入设为map<int,int>

如果我对上面的理解是正确的,那么将通过将引用保持为根节点来形成新的二叉搜索树。

还要考虑是否像这样插入outer_map.insert()然后在这里通过保持第一个元素作为root插入来形成另一个二元搜索树。

我对上述两点的理解是否正确?

如果是这样,这样做是否有效,因为我的一位朋友完成了这个模型。

示例代码

int main()
{
   using namespace std;

   map< int,map<int,int> > outer_map;

   outer_map[10].insert(make_pair<int,int>(1,2));
   outer_map[20].insert(make_pair<int,int>(3,4));
   outer_map[20].insert(make_pair<int,int>(5,6));


   map<int,int>  :: iterator in_iter ;
   map<int, map<int,int> > :: iterator out_iter;


   out_iter = outer_map.find(10);

   if(out_iter != outer_map.end())
   {
      printf("\n (out_iter->second).size :: %d ", (out_iter->second).size());
      for(in_iter =  (out_iter->second).begin() ; in_iter != (out_iter->second).end(); in_iter++ )
      {
         printf("\n Found");
      }
   }
  printf("\n out_map.size :: %d ", outer_map.size() );

    return 0;
}

0 个答案:

没有答案