Iterating over multi map

This is a discussion on Iterating over multi map within the C++ Programming forums, part of the General Programming Boards category; Hi, I have a problem trying to iterate over a multimap (or rather map within map). I have this: Code: ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    25

    Iterating over multi map

    Hi,

    I have a problem trying to iterate over a multimap (or rather map within map).

    I have this:

    Code:
    #include <map>
    
    using namespace std;
    
    int main(){
    map<string, map<string, string> > mymap;
    map<string, map<string, string> >::iterator itm;
    
    /* This is how I would iterate over a normal map;
    for( it=mymap.begin(); it!=mymap.end(); ++it)
    {
    cout << "first: \t" << it->first << "second: \t" << it->second << endl;
    }
    
    OR
    
    cout << mymap.find("ab")->second << endl;
    */
    
    return 0;
    }
    But for a map within a map it won't work.

    Can someone help?


    Thanks.

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by coderplus View Post
    But for a map within a map it won't work.
    What won't work? Please post an example of what you expect to work, and in what respect it is not working. Does it not compile, or does it just not produce the right results?

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    25
    That is obvious.

    As per example I have given, iterating over a normal map, trying to produce the same results with a map within a map doesn't work.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,302
    Iterating over the map will do exactly what it is supposed to do. In this instance each time around the iterator's "first" member will point to the map's key, which is a string, and its "second" will point to the value, which is a map<string, string>, thus perfectly iterating over the map.

    If you want it to do one of the many possible things that are something other than that, then you will need to write code to do whatever it is that you happen to want to do.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    25
    Yes, of course it can iterate fine over the first map but I want to iterate over the second map.

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by coderplus View Post
    Yes, of course it can iterate fine over the first map but I want to iterate over the second map.
    Iteration over the second map works the same way as for the outher map.
    Without you showing what is not working we will not get anywhere here.
    Kurt

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    25
    OK so if I had:

    Code:
    for( itm=mymap.begin(); itm!=mymap.end(); ++itm)
    
    {
    cout << "first: \t" << it->first << endl;
    
    }
    That to iterate over the first map, how would I get to the second map?

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by coderplus View Post
    OK so if I had:

    Code:
    for( itm=mymap.begin(); itm!=mymap.end(); ++itm)
    
    {
    cout << "first: \t" << it->first << endl;
    
    }
    That to iterate over the first map, how would I get to the second map?
    Do the same; just do it over the second map.

    (Hint: Show us code, or it looks an awful lot like you're trying to get us to do your homework for you.)

  9. #9
    Registered User
    Join Date
    Apr 2012
    Posts
    25
    It has nothing to do with homework.

    And I posted the code I was working on.

  10. #10
    Registered User
    Join Date
    Apr 2012
    Posts
    25
    Nevermind, I have figured it out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. iterating through hex numbers in c
    By f.g in forum C Programming
    Replies: 18
    Last Post: 08-24-2011, 06:35 PM
  2. Iterating through a multidimensional array
    By Angus in forum C Programming
    Replies: 20
    Last Post: 06-05-2009, 09:32 AM
  3. Iterating through a map that contains a vector
    By AngryStyro in forum C++ Programming
    Replies: 2
    Last Post: 06-08-2008, 05:01 AM
  4. Iterating through an array
    By MikeyIckey in forum C Programming
    Replies: 15
    Last Post: 11-10-2007, 09:26 PM
  5. Speed of iterating through STL containers
    By BobMcGee123 in forum C++ Programming
    Replies: 20
    Last Post: 06-13-2006, 10:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21