Shohreh
Shohreh

Reputation: 135

return std::unique_ptr containing std::map

I have a map of maps as follows:

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

and I need to return a refrence to one of the maps corresponding to an specified key from a function. I do not know how I should do this ,here is my code:

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);
}

Upvotes: 0

Views: 703

Answers (1)

billz
billz

Reputation: 45410

This is what I will do instead:

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");
}

Upvotes: 4

Related Questions