返回包含std :: map的std :: unique_ptr

时间:2013-01-08 05:51:47

标签: c++ stdmap unique-ptr

我有一张地图地图如下:

std::map<char,std::map<short,char>> my_maps;

我需要将一个refrence返回到与函数中指定键对应的映射之一。 我不知道该怎么做,这是我的代码:

bool GetMap(char key,std::unique_ptr<std::map<short,char>> my_map){


  auto m=my_maps.find(key);
  if(m!=my_maps.end()){

     my_map=m->second;
     // I have also tried this: my_map=my_maps[_commandcode];

    return(true);
  }
  return (false);
}

1 个答案:

答案 0 :(得分:4)

这就是我要做的事情:

std::map<short,char>& GetMap(char key, std::map<char,std::map<short,char>> &my_maps)
{
  auto m = my_maps.find(key);
  if (m != my_maps.end())
  {
    return m->second;
  }
  throw std::exception("not found");
}