-
Map of Maps
Hello everyone,
I have a map of maps :-
typedef map <string, map <string, int> > myMapofMaps;
myMapofMaps a;
I'm trying to insert an entry but without actually creating the inner map as an object. Is that possible at all ?
In other words, I want to do something like :-
a.insert(pair <string, map <string, int> >("AAA",(pair <string, int> ("A1", 1))));
As you can see in the inner map I'm not specifying the map name.
I need to do this this way because I don't know in advance how many inner maps there will be, so I cannot do :-
map <string, int> x1;
map <string, int> x2;
map <string, int> x3;
etc.
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.
-
with std::map you can do sth like
Code:
map<string,int> myMap1;
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.
Code:
map<string,int> createElement(string s, int i)
{
map<string,int> reval;
reval[s] = i;
return reval;
}
// now you can:
myMap2["'nother entry"] = createElement("innerMapKey",42);
Dunno whether this is any good to you...
DKyb-------------------------------
Life is a short warm moment -
Death is the long cold rest.
Pink Floyd
-------------------------------
-
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 :-
map<string,int> a1;
map<string,int> a2;
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 ?
Many thanks.
-
Can't you do something like this:
Code:
typedef map <string, map <string, int> > myMapofMaps;
myMapofMaps a;
a["AAA"]["A1"] = 10;
a["AAA"]["A2"] = 20;
a["AAA"]["A3"] = 20;
a["BBB"]["B1"] = 10;
//... etc
This code will create as many inner maps as you need.
-
Many thanks.
I just didn't see the obvious...
Thanks again.
Similar Threads
-
Replies: 2
Last Post: 09-04-2009, 11:54 AM
-
By alsoares in forum AJAX
Replies: 2
Last Post: 12-20-2007, 07:03 PM
-
Replies: 3
Last Post: 05-16-2007, 03:06 PM
-
By zion in forum VB Classic
Replies: 2
Last Post: 02-27-2006, 02:27 PM
-
By mark hembree in forum ASP.NET
Replies: 1
Last Post: 01-11-2001, 10:21 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks