DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Map of Maps

  1. #1
    Join Date
    May 2004

    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;

    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.

  2. #2
    Join Date
    Jan 2005
    with std::map you can do sth like

    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.

    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...
    Life is a short warm moment -
    Death is the long cold rest.
    Pink Floyd

  3. #3
    Join Date
    May 2004
    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.

  4. #4
    Join Date
    Aug 2009
    Can't you do something like this:
      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.

  5. #5
    Join Date
    May 2004
    Many thanks.

    I just didn't see the obvious...

    Thanks again.

Similar Threads

  1. Map of maps
    By ami in forum C++
    Replies: 2
    Last Post: 09-04-2009, 11:54 AM
  2. Swapping a map element (a DOM question)
    By alsoares in forum AJAX
    Replies: 2
    Last Post: 12-20-2007, 08:03 PM
  3. map and memory leak
    By WXY595 in forum C++
    Replies: 3
    Last Post: 05-16-2007, 03:06 PM
  4. Parse string with delimiters
    By zion in forum VB Classic
    Replies: 2
    Last Post: 02-27-2006, 03:27 PM
  5. map areas not passing Form object to java script
    By mark hembree in forum ASP.NET
    Replies: 1
    Last Post: 01-11-2001, 11: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
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center