I need the compiler to know that once I insert an entry to the outer map, then an inner map needs to be created automatically and I need to be able to access it later.
I tried putting the inner map into a vector, but I still cannot make_pairs this way. I still need to find a way of inserting to a map without actually manually creating it myself.
Does this make sense ? Can I let the compiler create maps for me at run time?
Many thanks for your help.
09-09-2009, 10:24 AM
with std::map you can do sth like
myMap1["entry in first Map"] = 1;
map<string, map<string,int> > myMap2;
myMap2["entry in second Map"]/* = map<string,int>()*/; // the commented out stuff is not needed, the [..] operator insert a default element anyway
But if you want to add any other than the default element, I would create a little function that returns a map<string,int> object.
map<string,int> createElement(string s, int i)
reval[s] = i;
// now you can:
myMap2["'nother entry"] = createElement("innerMapKey",42);
Dunno whether this is any good to you...
09-09-2009, 11:01 AM
Many thanks for that.
Can you please tell me how would you insert into the map created in createElement ? I mean, as I was saying prior to Run time, I don't know in advance how many map<string,int> maps there will be, so how I can name them ? I mean, we normally do :-
and from what I understand you are suggest doing :-
map<string,int> newMap = createElement(...);
but the point is, I cannot hard-code "newMap" as I don't know in advance ghow many maps I want to create. Should I do it with a class that returns the map you're suggesting and then do: new createElement(...) ?
If so, will a pointer to this class return me the "right map" every time ?