CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2010
    Posts
    5

    Iterating Through a Map Within a Map

    I have a map of maps but I'm not sure how to iterate through the inner one.

    Here's what I have so far:

    The type:
    Code:
    // It's a map...of maps. INCEPTION
    typedef map <string, map <string, string>> iniTree;
    The attempted iteration; the outer one seems to be fine but I'm at a loss for what to do for the inner one.
    Code:
    iniTree::iterator GroupPos;
    iniTree::iterator KeyPos;
    for (GroupPos = myMap.begin(); GroupPos != myMap.end(); GroupPos++) {
    	for (KeyPos = myMap[GroupPos].begin(); KeyPos != myMap[GroupPos].end(); KeyPos++) {
    
    	}
    }
    If anyone can point me in the right direction, I'd really appreciate it.

    -LBD

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Iterating Through a Map Within a Map

    Quote Originally Posted by LeonsBuddyDave View Post
    I have a map of maps but I'm not sure how to iterate through the inner one.
    Code:
    #include <string>
    #include <map>
    
    typedef std::map< std::string, std::string> MapStringToString;
    typedef std::map< std::string, MapStringToString > iniTree;
    
    iniTree myMap;
    
    void foo()
    {
        iniTree::iterator GroupPos;
        iniTree::iterator KeyPos;
        for (GroupPos = myMap.begin(); GroupPos != myMap.end(); ++GroupPos) 
        {
             MapStringToString& innerMap = GroupPos->second;
             for (MapStringToString::iterator it = innerMap.begin(); it !=  innerMap.end(); ++it )
             {
                //...
             }
         }
    }
    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Dec 2010
    Posts
    5

    Re: Iterating Through a Map Within a Map

    Thank you very much.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured