943,840 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5114
  • C++ RSS
Mar 27th, 2009
0

In dealing with a map of maps...

Expand Post »
Hello,

If I have code like such:

c++ Syntax (Toggle Plain Text)
  1. map <string, map <string, int> > foo;
  2. map <string, map <string, int> >::iterator bar;

How to access the value of the nested map? Normally I access a map's value like such:

c++ Syntax (Toggle Plain Text)
  1. bar->second;

To that end, I tried doing this on my nested map:
c++ Syntax (Toggle Plain Text)
  1. bar->second->second;

But this gave me complier errors. What is the correct syntax for this?
Similar Threads
Reputation Points: 10
Solved Threads: 7
Junior Poster in Training
ItecKid is offline Offline
70 posts
since Dec 2008
Mar 27th, 2009
0

Re: In dealing with a map of maps...

Think of bar->second as a map <string, int> , so simply ...
C++ Syntax (Toggle Plain Text)
  1. bar->second["abc"] = 123;
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007
Mar 27th, 2009
0

Re: In dealing with a map of maps...

If you want to read through all your maps in your map (...), you need two different iterators. One for a map <string, int> and one for a map<string, map<string, int> >.
So something like:
C++ Syntax (Toggle Plain Text)
  1. map <string, map <string, int> > foo;
  2. // fill map
  3. map <string, map <string, int> >::iterator outerit;
  4. map <string, int>::iterator innerit;
  5. for (outerit = foo.begin(); outerit != foo.end(); ++outerit){
  6. for (innerit = outerit->second.begin(); innerit != outerit->second.end(); ++innerit){
  7. cout << outerit->first << " " << innerit->first << " " << innerit->second << "\n";
  8. }
  9. }
Last edited by Nick Evan; Mar 27th, 2009 at 8:06 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is online now Online
4,132 posts
since Oct 2006
Mar 27th, 2009
0

Re: In dealing with a map of maps...

Click to Expand / Collapse  Quote originally posted by niek_e ...
If you want to read through all your maps in your map (...), you need two different iterators. One for a map <string, int> and one for a map<string, map<string, int> >.
So something like:
C++ Syntax (Toggle Plain Text)
  1. map <string, map <string, int> > foo;
  2. // fill map
  3. map <string, map <string, int> >::iterator outerit;
  4. map <string, int>::iterator innerit;
  5. for (outerit = foo.begin(); outerit != foo.end(); ++outerit){
  6. for (innerit = outerit->second.begin(); innerit != outerit->second.end(); ++innerit){
  7. cout << outerit->first << " " << innerit->first << " " << innerit->second << "\n";
  8. }
  9. }
Hello,

Thank you very much for your reply. I have one more question. Due to assignment parameters, the performance of this function must be sub-linear. (i.e, no for loops). So if I have code above, and do this:

c++ Syntax (Toggle Plain Text)
  1. outerit = foo.find (some string);

How to move the position of innerit to the value of the key of outerit?
Reputation Points: 10
Solved Threads: 7
Junior Poster in Training
ItecKid is offline Offline
70 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: queues
Next Thread in C++ Forum Timeline: backgroundWorker2->CancelAsync();





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC