mardi 1 décembre 2015

How to create new entry in std::map without copying the entry value - no pointers

I have a map:

std::map<std::string, MyDataContainer>

The MyDataContainer is some class or struct (doesn't matter). Now I want to create a new data container. Let's say I wanna do it with default constructor available:

// This is valid, MyDataContainer doesn't need constructor arguments
MyDataConstructor example;
// The map definition
std::map<std::string, MyDataContainer> map;
std::string name("entry");
// This copies value of `example`
map[name] = example;
// Below, I want to create entry without copy:
std::string name2 = "nocopy"
// This is pseudo-syntax
map.createEmptyEntry(name2);

Is there way to do that? To skip creating helper variable when I just wanna initialize it in map? And would it be possible to do it with constructor arguments?

I think this question also applies to other std containers, like or .

Aucun commentaire:

Enregistrer un commentaire