vendredi 1 février 2019

Correct way to create and manage look up containers

correct way to create look up maps for instances. I have a structure defined Node as follows

struct Node
{
    int32_t id;
    std::string name;
    ...
}

i want to create 2 look up maps, on based on id and another based on name. There are other attributes in the Node which also needs look up maps but those are dynamic so not every Node instance will have a look entry into those additional maps.

with just one look up map I was planning to create something like

typedef std::unoredered_map<int32_t, std::unique_ptr <Node> > NodesById;

I reason being just I can get this deleted by just erase or [id] = new 'overwrite!' operation and don't have to worry about it. But then how can I add the same Node instance to say another map

typedef std::unoredered_map<std::string, std::unique_ptr <Node> > NodesByName;

I cannot put same Node instance into to unique_ptr. So my question is what is the correct way to store Node instances into multiple look up tables and still achieve smart memory management.

Aucun commentaire:

Enregistrer un commentaire